|
| 1 | +/** |
| 2 | + * 1169. Invalid Transactions |
| 3 | + * https://leetcode.com/problems/invalid-transactions/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * A transaction is possibly invalid if: |
| 7 | + * - the amount exceeds $1000, or; |
| 8 | + * - if it occurs within (and including) 60 minutes of another transaction with the same name in |
| 9 | + * a different city. |
| 10 | + * |
| 11 | + * You are given an array of strings transaction where transactions[i] consists of comma-separated |
| 12 | + * values representing the name, time (in minutes), amount, and city of the transaction. |
| 13 | + * |
| 14 | + * Return a list of transactions that are possibly invalid. You may return the answer in any order. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @param {string[]} transactions |
| 19 | + * @return {string[]} |
| 20 | + */ |
| 21 | +var invalidTransactions = function(transactions) { |
| 22 | + const parsed = transactions.map(t => { |
| 23 | + const [name, time, amount, city] = t.split(','); |
| 24 | + return { name, time: Number(time), amount: Number(amount), city }; |
| 25 | + }); |
| 26 | + |
| 27 | + const invalid = new Set(); |
| 28 | + |
| 29 | + for (let i = 0; i < parsed.length; i++) { |
| 30 | + const current = parsed[i]; |
| 31 | + if (current.amount > 1000) { |
| 32 | + invalid.add(i); |
| 33 | + } |
| 34 | + |
| 35 | + for (let j = 0; j < parsed.length; j++) { |
| 36 | + const other = parsed[j]; |
| 37 | + if (i !== j && current.name === other.name |
| 38 | + && Math.abs(current.time - other.time) <= 60 && current.city !== other.city) { |
| 39 | + invalid.add(i); |
| 40 | + invalid.add(j); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return [...invalid].map(index => transactions[index]); |
| 46 | +}; |
0 commit comments