You are viewing a single comment's thread. Return to all comments →
This solution works
static List<Integer> freqQuery(List<int[]> queries) { final Map<Integer, Integer> valueToFreq = new HashMap<>(); final Map<Integer, Integer> freqToOccurrence = new HashMap<>(); final List<Integer> frequencies = new ArrayList<>(); int key; int value; Integer oldFreq; Integer newFreq; Integer oldOccurrence; Integer newOccurrence; for (int[] query : queries) { key = query[0]; value = query[1]; if (key == 3) { if (value == 0) { frequencies.add(1); } frequencies.add(freqToOccurrence.get(value) == null ? 0 : 1); } else { oldFreq = valueToFreq.get(value); oldFreq = oldFreq == null ? 0 : oldFreq; oldOccurrence = freqToOccurrence.get(oldFreq); oldOccurrence = oldOccurrence == null ? 0 : oldOccurrence; if (key == 1) { newFreq = oldFreq + 1; } else { newFreq = oldFreq - 1; } newOccurrence = freqToOccurrence.get(newFreq); newOccurrence = newOccurrence == null ? 0 : newOccurrence; if (newFreq < 1) { valueToFreq.remove(value); } else { valueToFreq.put(value, newFreq); } if ((oldOccurrence - 1) < 1) { freqToOccurrence.remove(oldFreq); } else { freqToOccurrence.put(oldFreq, oldOccurrence - 1); } freqToOccurrence.put(newFreq, newOccurrence + 1); } } return frequencies; }
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 →
This solution works