You are viewing a single comment's thread. Return to all comments →
Why doesn't this code which runs in O(n) pass the test cases 10, 11, 12 and 13? It times out.
static List<Integer> freqQuery(List<int[]> queries) { List<Integer> result = new ArrayList<Integer>(); HashMap<Integer, Integer> map = new HashMap<>(); HashMap<Integer, Integer> freq = new HashMap<>(); for (int[] q: queries) { System.out.println("\nIndex: " + _count ++); int x = q[0]; int y = q[1]; Integer count = map.getOrDefault(y, 0); Integer _freq = freq.getOrDefault(count, 0); if (x == 1) { freq.put(count, _freq > 0 ? (_freq - 1): 0); freq.put(count + 1, freq.getOrDefault(count + 1, 0) + 1); map.put(y, count + 1); } else if (x == 2) { if (count != 0) { freq.put(count, _freq > 0 ? (_freq - 1): 0); freq.put(count - 1, freq.getOrDefault(count - 1, 0) + 1); map.put(y, count - 1); } } else if (x == 3) { result.add(freq.getOrDefault(y, 0) > 0 ? 1: 0); } } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
Frequency Queries
You are viewing a single comment's thread. Return to all comments →
Why doesn't this code which runs in O(n) pass the test cases 10, 11, 12 and 13? It times out.