File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Interview Preparation Kit/Dictionaries and Hashmaps/frequency-queries Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ from collections import defaultdict
2
+ from typing import List
3
+
4
+
5
+ def freqQuery (queries : List [int ]) -> List [int ]:
6
+ """
7
+ :param queries: a 2-d array of integers
8
+ :return: the results of queries of type 3
9
+ """
10
+ results = []
11
+ quantities = dict ()
12
+ frequencies = defaultdict (set )
13
+
14
+ for command , value in queries :
15
+ frequency = quantities .get (value , 0 )
16
+ if command == 1 :
17
+ quantities [value ] = frequency + 1
18
+ frequencies [frequency ].discard (value )
19
+ frequencies [frequency + 1 ].add (value )
20
+ elif command == 2 :
21
+ quantities [value ] = max (0 , frequency - 1 )
22
+ frequencies [frequency ].discard (value )
23
+ frequencies [frequency - 1 ].add (value )
24
+ elif command == 3 :
25
+ results .append (1 if frequencies [value ] else 0 )
26
+ else :
27
+ raise Exception ("Query type out of query types" )
28
+ return results
29
+
30
+
31
+ if __name__ == '__main__' :
32
+ q = []
33
+
34
+ for _ in range (int (input ().strip ())):
35
+ q .append (list (map (int , input ().rstrip ().split ())))
36
+
37
+ print (freqQuery (q ))
38
+
You can’t perform that action at this time.
0 commit comments