-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1169-invalid-transactions.js
46 lines (41 loc) · 1.37 KB
/
1169-invalid-transactions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 1169. Invalid Transactions
* https://leetcode.com/problems/invalid-transactions/
* Difficulty: Medium
*
* A transaction is possibly invalid if:
* - the amount exceeds $1000, or;
* - 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.
*/
/**
* @param {string[]} transactions
* @return {string[]}
*/
var invalidTransactions = function(transactions) {
const parsed = transactions.map(t => {
const [name, time, amount, city] = t.split(',');
return { name, time: Number(time), amount: Number(amount), city };
});
const invalid = new Set();
for (let i = 0; i < parsed.length; i++) {
const current = parsed[i];
if (current.amount > 1000) {
invalid.add(i);
}
for (let j = 0; j < parsed.length; j++) {
const other = parsed[j];
if (i !== j && current.name === other.name
&& Math.abs(current.time - other.time) <= 60 && current.city !== other.city) {
invalid.add(i);
invalid.add(j);
}
}
}
return [...invalid].map(index => transactions[index]);
};