Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def freqQuery(queries):
- result = []
- d = {}
- d_freq = {}
- """
- example - sample 2
- d = number:frequency
- {4: 2, 5: 2, 3: 0, 1:1}
- dict = frequency:numbers
- {0: {3}, 2: {4, 5}, 1:{1}}
- """
- for q in queries:
- if q[0] == 1:
- # add 1 to d[q[1]]
- d[q[1]] = d.get(q[1], 0) + 1
- # if d[q[1]] is not yet in d_freq then put an empty set as it's value
- d_freq[d[q[1]]] = d_freq.get(d[q[1]], set([]))
- # add frequency:number to dict d
- d_freq[d[q[1]]].add(q[1])
- # if d[q[1]]-1 is not yet in d_freq then put an empty set as it's value
- d_freq[d[q[1]] - 1] = d_freq.get(d[q[1]] - 1, set([]))
- # discard the number at d[q[1]]-1
- d_freq[d[q[1]] - 1].discard(q[1])
- elif q[0] == 2:
- if d.get(q[1], 0):
- # if d[q[1]] is not yet in d_freq then put an empty set as it's value
- d_freq[d[q[1]]] = d_freq.get(d[q[1]], set([]))
- # discard the number at d[q[1]]
- d_freq[d[q[1]]].discard(q[1])
- # if d[q[1]-1] is not yet in d_freq then put an empty set as it's value
- d_freq[d[q[1]] - 1] = d_freq.get(d[q[1]] - 1, set([]))
- # add the number at d[q[1]]-1
- d_freq[d[q[1]] - 1].add(q[1])
- # minus 1 to d[q[1]]
- d[q[1]] = max(0, d.get(q[1], 0) - 1)
- else:
- if d_freq.get(q[1], 0):
- result.append(1)
- else:
- result.append(0)
- return result
- q = [[1, 3], [2, 3], [3, 2], [1, 4], [1, 5], [1, 5], [1, 4], [3, 2], [2, 4], [3, 2]]
- print(freqQuery(q))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement