-
Notifications
You must be signed in to change notification settings - Fork 0
/
1169. Invalid Transactions.py
executable file
·42 lines (34 loc) · 1.29 KB
/
1169. Invalid Transactions.py
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
'''
brute force:
1. convert transactions list to a list of Transaction objects
2. sort the list by their time
3. for -> list:
1. if amount > 1000: add it
2. for -> remaining list:
1. if condition 2, add it
'''
class Transaction:
def __init__(self, name, time, amount, city):
self.name = name
self.time = int(time)
self.amount = int(amount)
self.city = city
class Solution:
def invalidTransactions(self, transactions: List[str]) -> List[str]:
# O(n)
transactions = [Transaction(*t.split(',')) for t in transactions]
# O(nlogn)
transactions.sort(key=lambda t: t.time)
invalids = set()
# O(n ^ 2)
for i in range(len(transactions)):
t1 = transactions[i]
if t1.amount > 1000:
invalids.add(f'{t1.name},{t1.time},{t1.amount},{t1.city}')
for j in range(i + 1, len(transactions)):
t2 = transactions[j]
if t2.name == t1.name and t2.time - t1.time <= 60 and t2.city != t1.city:
invalids.add(f'{t1.name},{t1.time},{t1.amount},{t1.city}')
invalids.add(f'{t2.name},{t2.time},{t2.amount},{t2.city}')
# O(n)
return list(invalids)