File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change 3
3
4
4
from typing import Dict
5
5
6
+ __INITIAL__ = 0
7
+
8
+ __INSERT__ = 1
9
+ __DELETE__ = 2
10
+ __SELECT__ = 3
11
+
6
12
7
13
def freq_query (queries ):
8
14
result = []
@@ -11,12 +17,13 @@ def freq_query(queries):
11
17
for query in queries :
12
18
operation = query [0 ]
13
19
data = query [1 ]
20
+ current = data_map .get (data , __INITIAL__ )
14
21
15
- if operation == 1 : # insert
16
- data_map [data ] = data_map . get ( data , 0 ) + 1
17
- elif operation == 2 and data_map . get ( data , 0 ) > 0 : # delete
18
- data_map [data ] -= 1
19
- elif operation == 3 : # "select"
22
+ if operation == __INSERT__ :
23
+ data_map [data ] = current + 1
24
+ elif operation == __DELETE__ :
25
+ data_map [data ] = max ( current - 1 , 0 )
26
+ elif operation == __SELECT__ :
20
27
for value in data_map .values ():
21
28
if value == data :
22
29
result .append (1 )
You can’t perform that action at this time.
0 commit comments