Skip to content

Instantly share code, notes, and snippets.

@huzaifaaak
Created November 24, 2020 18:51
Show Gist options
  • Save huzaifaaak/b85aba56c2a92d494d053e2d61a1454b to your computer and use it in GitHub Desktop.
Save huzaifaaak/b85aba56c2a92d494d053e2d61a1454b to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the freqQuery function below.
def freqQuery(queries):
answers = []
freq = {}
data = []
test = [0] * 100000
if len(queries) == 0:
return []
for i in range(len(queries)):
action = queries[i][0]
value = queries[i][1]
if value == 0:
continue
if action == 1:
data.append(value)
if value not in freq:
freq[value] = 1
test[1] = 1
else:
lastIndex = freq[value] #1
freq[value] += 1
test[lastIndex] -= 1
lastIndex += 1
test[lastIndex] += 1 #lastindex + 1 becomes 2
elif action == 2:
if value in data:
index = data.index(value)
data.pop(index)
if value in freq:
lastIndex = freq[value] #1
freq[value] -= 1
# test[lastIndex] = 0
lastIndex -= 1
test[lastIndex] -= 1
else:
print(test, test[value])
if test[value] > 0:
answers.append(1)
else:
answers.append(0)
print(test)
return answers
#[1, 9, 1, 2, 1, 4, 5]
#
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment