- 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
- 37 lines of Java from the credited upstream file 1169.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- 3 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.
1class Solution {2 public List<String> invalidTransactions(String[] transactions) {3 List<String> ans = new ArrayList<>();4 Map<String, List<Trans>> nameToTrans = new HashMap<>();5 6 for (final String t : transactions) {7 Trans trans = getTrans(t);8 nameToTrans.putIfAbsent(trans.name, new ArrayList<>());9 nameToTrans.get(trans.name).add(trans);10 }11 12 for (final String t : transactions) {13 Trans currTrans = getTrans(t);14 if (currTrans.amount > 1000) {15 ans.add(t);16 } else if (nameToTrans.containsKey(currTrans.name)) {17 18 19 for (Trans trans : nameToTrans.get(currTrans.name))20 if (Math.abs(trans.time - currTrans.time) <= 60 && !trans.city.equals(currTrans.city)) {21 ans.add(t);22 break;23 }24 }25 }26 27 return ans;28 }29 30 private record Trans(String name, int time, int amount, String city) {}31 32 private Trans getTrans(final String t) {33 String[] s = t.split(","); 34 return new Trans(s[0], Integer.parseInt(s[1]), Integer.parseInt(s[2]), s[3]);35 }36}37