- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 48 lines of C++ from the credited upstream file 1169.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 4 loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1struct Transaction {2 string name;3 int time;4 int amount;5 string city;6};7 8class Solution {9 public:10 vector<string> invalidTransactions(vector<string>& transactions) {11 vector<string> ans;12 unordered_map<string, vector<Transaction>> nameToTransactions;13 14 for (const string& t : transactions) {15 const Transaction transaction = getTransaction(t);16 nameToTransactions[transaction.name].push_back(transaction);17 }18 19 for (const string& t : transactions) {20 const Transaction currTransaction = getTransaction(t);21 if (currTransaction.amount > 1000) {22 ans.push_back(t);23 } else if (const auto it = nameToTransactions.find(currTransaction.name);24 it != nameToTransactions.cend()) {25 26 27 for (Transaction transaction : it->second)28 if (abs(transaction.time - currTransaction.time) <= 60 &&29 transaction.city != currTransaction.city) {30 ans.push_back(t);31 break;32 }33 }34 }35 36 return ans;37 }38 39 private:40 Transaction getTransaction(const string& t) {41 istringstream iss(t);42 vector<string> s(4, "");43 for (int i = 0; getline(iss, s[i++], ',');)44 ;45 return {s[0], stoi(s[1]), stoi(s[2]), s[3]}; 46 }47};48