if it occurs within (and including) 60 minutes of another transaction with the same name in a different city.
You are given an array of strings transaction where transactions[i]
consists of comma-separated values representing the name, time (in minutes), amount, and city of the transaction.
Return a list of transactions that are possibly invalid. You may return the answer in any order.
Input: transactions =["alice,20,800,mtv","alice,50,100,beijing"]Output: ["alice,20,800,mtv","alice,50,100,beijing"]Explanation: The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and isin a different city. Similarly the second one is invalid too.
We need to check two conditions for each transaction: if the amount exceeds 1000, or if there is another transaction with the same name in a different city within 60 minutes. We can use a hash map to group transactions by name and compare each pair for the second condition.
classSolution {
public List<String>invalidTransactions(String[] transactions) {
classTxn { String name, city, orig; int time, amt; }
List<Txn> txns =new ArrayList<>();
for (String s : transactions) {
String[] p = s.split(",");
Txn t =new Txn();
t.name= p[0]; t.time= Integer.parseInt(p[1]); t.amt= Integer.parseInt(p[2]); t.city= p[3]; t.orig= s;
txns.add(t);
}
int n = txns.size();
boolean[] bad =newboolean[n];
Map<String, List<Integer>> mp =new HashMap<>();
for (int i = 0; i < n; ++i) {
if (txns.get(i).amt> 1000) bad[i]=true;
mp.computeIfAbsent(txns.get(i).name, k ->new ArrayList<>()).add(i);
}
for (var idxs : mp.values()) {
for (int i = 0; i < idxs.size(); ++i) {
for (int j = i+1; j < idxs.size(); ++j) {
var a = txns.get(idxs.get(i)), b = txns.get(idxs.get(j));
if (!a.city.equals(b.city) && Math.abs(a.time- b.time) <= 60) {
bad[idxs.get(i)]= bad[idxs.get(j)]=true;
}
}
}
}
List<String> ans =new ArrayList<>();
for (int i = 0; i < n; ++i) if (bad[i]) ans.add(txns.get(i).orig);
return ans;
}
}
classSolution {
funinvalidTransactions(transactions: Array<String>): List<String> {
dataclassTxn(val name: String, val city: String, val orig: String, val time: Int, val amt: Int)
val txns = transactions.map {
val p = it.split(",")
Txn(p[0], p[3], it, p[1].toInt(), p[2].toInt())
}
val n = txns.size
val bad = BooleanArray(n)
val mp = mutableMapOf<String, MutableList<Int>>()
for (i in0 until n) {
if (txns[i].amt > 1000) bad[i] = true mp.getOrPut(txns[i].name) { mutableListOf() }.add(i)
}
for (idxs in mp.values) {
for (i in idxs.indices) {
for (j in i+1 until idxs.size) {
val a = txns[idxs[i]]; val b = txns[idxs[j]]
if (a.city != b.city && kotlin.math.abs(a.time - b.time) <=60) {
bad[idxs[i]] = true; bad[idxs[j]] = true }
}
}
}
return (0 until n).filter { bad[it] }.map { txns[it].orig }
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
definvalid_transactions(transactions: list[str]) -> list[str]:
txns = []
for s in transactions:
name, time, amt, city = s.split(',')
txns.append((name, int(time), int(amt), city, s))
n = len(txns)
bad = [False] * n
mp = {}
for i, (name, time, amt, city, _) in enumerate(txns):
if amt >1000:
bad[i] =True mp.setdefault(name, []).append(i)
for idxs in mp.values():
for i in range(len(idxs)):
for j in range(i+1, len(idxs)):
a = txns[idxs[i]]; b = txns[idxs[j]]
if a[3] != b[3] and abs(a[1] - b[1]) <=60:
bad[idxs[i]] = bad[idxs[j]] =Truereturn [txns[i][4] for i in range(n) if bad[i]]
use std::collections::HashMap;
fninvalid_transactions(transactions: Vec<String>) -> Vec<String> {
letmut txns =vec![];
for s in&transactions {
let p: Vec<&str>= s.split(',').collect();
txns.push((p[0], p[1].parse::<i32>().unwrap(), p[2].parse::<i32>().unwrap(), p[3], s));
}
let n = txns.len();
letmut bad =vec![false; n];
letmut mp: HashMap<&str, Vec<usize>>= HashMap::new();
for (i, (name, time, amt, city, _)) in txns.iter().enumerate() {
if*amt >1000 { bad[i] =true; }
mp.entry(name).or_default().push(i);
}
for idxs in mp.values() {
for i in0..idxs.len() {
for j in i+1..idxs.len() {
let a =&txns[idxs[i]]; let b =&txns[idxs[j]];
if a.3!= b.3&& (a.1- b.1).abs() <=60 {
bad[idxs[i]] =true; bad[idxs[j]] =true;
}
}
}
}
letmut ans =vec![];
for i in0..n {
if bad[i] { ans.push(txns[i].4.to_string()); }
}
ans
}