|
| 1 | +""" |
| 2 | + A transaction is possibly invalid if: |
| 3 | + - the amount exceeds $1000, or; |
| 4 | + - if it occurs within (and including) 60 minutes of another transaction |
| 5 | + with the same name in a different city. |
| 6 | +
|
| 7 | + Each transaction string transactions[i] consists of comma separated values |
| 8 | + representing the name, time (in minutes), amount, and city of the |
| 9 | + transaction. |
| 10 | +
|
| 11 | + Given a list of transactions, return a list of transactions that are |
| 12 | + possibly invalid. You may return the answer in any order. |
| 13 | +
|
| 14 | + Example: |
| 15 | + Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"] |
| 16 | + Output: ["alice,20,800,mtv","alice,50,100,beijing"] |
| 17 | + Explanation: The first transaction is invalid because the second |
| 18 | + transaction occurs within a difference of 60 minutes, have |
| 19 | + the same name and is in a different city. Similarly the |
| 20 | + second one is invalid too. |
| 21 | +
|
| 22 | + Example: |
| 23 | + Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"] |
| 24 | + Output: ["alice,50,1200,mtv"] |
| 25 | +
|
| 26 | + Example: |
| 27 | + Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"] |
| 28 | + Output: ["bob,50,1200,mtv"] |
| 29 | +
|
| 30 | + Constraints: |
| 31 | + - transactions.length <= 1000 |
| 32 | + - Each transactions[i] takes the form "{name},{time},{amount},{city}" |
| 33 | + - Each {name} and {city} consist of lowercase English letters, and have |
| 34 | + lengths between 1 and 10. |
| 35 | + - Each {time} consist of digits, and represent an integer between 0 |
| 36 | + and 1000. |
| 37 | + - Each {amount} consist of digits, and represent an integer between 0 |
| 38 | + and 2000. |
| 39 | +""" |
| 40 | +#Difficulty: Medium |
| 41 | +#33 / 33 test cases passed. |
| 42 | +#Runtime: 640 ms |
| 43 | +#Memory Usage: 14.6 MB |
| 44 | + |
| 45 | +#Runtime: 640 ms, faster than 19.77% of Python3 online submissions for Invalid Transactions. |
| 46 | +#Memory Usage: 14.6 MB, less than 49.87% of Python3 online submissions for Invalid Transactions. |
| 47 | + |
| 48 | +class Solution: |
| 49 | + def invalidTransactions(self, transactions: List[str]) -> List[str]: |
| 50 | + db = {} |
| 51 | + possibly_invalid = [] |
| 52 | + for transaction in transactions: |
| 53 | + name, time, amount, location = transaction.split(',') |
| 54 | + if name not in db: |
| 55 | + db[name] = [time], [amount], [location] |
| 56 | + else: |
| 57 | + for i in range(len(db[name][0])): |
| 58 | + if abs(int(time) - int(db[name][0][i])) <= 60 and location != db[name][2][i]: |
| 59 | + prev = name+','+db[name][0][i]+','+db[name][1][i]+','+db[name][2][i] |
| 60 | + if prev not in possibly_invalid: |
| 61 | + possibly_invalid.append(prev) |
| 62 | + if transaction not in possibly_invalid: |
| 63 | + possibly_invalid.append(transaction) |
| 64 | + db[name][0].append(time) |
| 65 | + db[name][1].append(amount) |
| 66 | + db[name][2].append(location) |
| 67 | + if int(amount) > 1000 and transaction not in possibly_invalid: |
| 68 | + possibly_invalid.append(transaction) |
| 69 | + return possibly_invalid |
0 commit comments