-
Notifications
You must be signed in to change notification settings - Fork 183
/
05 - Frequency Queries.py
53 lines (44 loc) · 1.2 KB
/
05 - Frequency Queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/frequency-queries/problem
# Difficulty: Medium
# Max Score: 40
# Language: Python
# ========================
# Solution
# ========================
import os
# Complete the freqQuery function below.
def freqQuery(queries):
count = dict()
result = list()
for q in queries:
if q[0] == 1:
try:
count[q[1]] += 1
except:
count[q[1]] = 1
elif q[0] == 2:
try:
count[q[1]] -= 1
if count[q[1]] == 0:
del count[q[1]]
except:
continue
else:
if q[1] in set(count.values()):
result.append('1')
else:
result.append('0')
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
q = int(input().strip())
queries = []
for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))
ans = freqQuery(queries)
fptr.write('\n'.join(map(str, ans)))
fptr.write('\n')
fptr.close()