You are viewing a single comment's thread. Return to all comments →
O(n) Time solution - track using two data structures
def freqQuery(queries): avail_map = {} freq_map = {} output_arr = [] for o,e in queries: if o == 1: avail_map[e] = avail_map.get(e,0) + 1 freq_map[avail_map[e]] = freq_map.get(avail_map[e],0) + 1 if (avail_map[e] - 1) in freq_map: freq_map[avail_map[e] - 1] -= 1 elif o == 2 and (e in avail_map) and (avail_map[e] > 0): freq_map[avail_map[e]] -= 1 avail_map[e] -= 1 freq_map[avail_map[e]] = freq_map.get(avail_map[e],0) + 1 elif o == 3: if e in freq_map and freq_map[e] > 0: output_arr.append(1) else: output_arr.append(0) return output_arr
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 →
O(n) Time solution - track using two data structures