-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1169.invalid-transactions.cpp
More file actions
79 lines (75 loc) · 2.16 KB
/
1169.invalid-transactions.cpp
File metadata and controls
79 lines (75 loc) · 2.16 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* @lc app=leetcode id=1169 lang=cpp
*
* [1169] Invalid Transactions
*/
// @lc code=start
struct Transaction {
int amount;
int time;
string city;
string name;
Transaction(int _time = 0, int _amount = 0, string _city = "") : amount(_amount), time(_time), city(_city) {}
Transaction(string trans) {
int len = trans.length();
int pos = 0;
while(trans[pos] != ',') pos += 1;
name = trans.substr(0, pos);
time = 0;
pos += 1;
while(trans[pos] != ',') {
time *= 10;
time += trans[pos] - '0';
pos += 1;
}
amount = 0;
pos += 1;
while(trans[pos] != ',') {
amount *= 10;
amount += trans[pos] - '0';
pos += 1;
}
pos += 1;
city = trans.substr(pos);
}
string to_string() {
return name + "," + ::to_string(time) + "," + ::to_string(amount) + "," + city;
}
bool operator<(const Transaction& another) const { return this->time < another.time; }
};
class Solution {
unordered_map<string, vector<Transaction>> transactions;
public:
vector<string> invalidTransactions(vector<string>& transactionStrings) {
for(auto &tx : transactionStrings) {
auto transaction = Transaction(tx);
transactions[transaction.name].push_back(transaction);
}
vector<string> answer;
for(auto &[name, transactions] : transactions) {
int len = transactions.size();
sort(transactions.begin(), transactions.end());
vector<bool> invalid(len);
for(int i = 0; i < len; ++i) {
// cout << transactions[i].to_string() << endl;
bool ok = true;
for(int j = i - 1; j >= 0 && transactions[i].time - transactions[j].time <= 60; --j) {
if(transactions[i].city != transactions[j].city) {
invalid[j] = true;
ok = false;
}
}
if(!ok || transactions[i].amount > 1000) invalid[i] = true;
}
for(int i = 0; i < len; ++i) {
if(invalid[i]) answer.push_back(transactions[i].to_string());
}
}
return answer;
}
};
// Accepted
// 36/36 cases passed (20 ms)
// Your runtime beats 87.55 % of cpp submissions
// Your memory usage beats 75.97 % of cpp submissions (13.3 MB)
// @lc code=end