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.
Constraints:
transactions.length <= 1000
Each transactions[i] takes the form “{name},{time},{amount},{city}”
Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
Each {time} consist of digits, and represent an integer between 0 and 1000.
Each {amount} consist of digits, and represent an integer between 0 and 2000.
boolcmp(const Data &d1, const Data &d2){ return d1.time < d2.time; }
classSolution { private: Data getData(string &str){ Data res; stringstream ss; int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == ',') { switch (cnt) { case0: res.name = ss.str(); break; case1: res.time = stoi(ss.str()); break; case2: res.amount = stoi(ss.str()); res.city = str.substr(i + 1); break; } cnt++; ss.str(""); } else { ss << str[i]; } } res.isPushed = false;
return res; }
public: vector<string> invalidTransactions(vector<string> &transactions){ vector<string> res; map<string, vector<Data>> myMap; for (int i = 0; i < transactions.size(); i++) { Data data = getData(transactions[i]); myMap[data.name].push_back(data); }
for (auto it = myMap.begin(); it != myMap.end(); it++) { sort(it->second.begin(), it->second.end(), cmp); for (int i = it->second.size() - 1, j = i - 1; i >= 0; --i) { bool flag = false; for (; j >= 0 && it->second[i].time - it->second[j].time <= 60; --j) { if (it->second[j].city != it->second[i].city) { if (!it->second[j].isPushed) { res.push_back(it->second[j].toString()); it->second[j].isPushed = true; } flag = true; } } if (!it->second[i].isPushed && (flag || it->second[i].amount > 1000)) { res.push_back(it->second[i].toString()); it->second[i].isPushed = true; } } }