We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I believe this test case is wrong. In order to print the final number 20842598
the input needs one more line with content '3'. Otherwise the number is never printed and the case fails.
from heapq import heappush, heappop
h= []
p = set()
data = [input().split(' ') for _ in range(int(input()))]
for idx in range(len(data)):
records = data[idx]
if records[0] == '1':
# p.add(records[1],heappush(h, records[1]))
p.add(int(records[1]))
heappush(h, int(records[1]))
elif records[0] == '2':
p.discard(int(records[1]))
elif records[0] == '3':
while h[0] not in p:
heappop(h)
print(h[0])
I believe this test case is wrong. In order to print the final number 20842598 the input needs one more line with content '3'. Otherwise the number is never printed and the case fails.
22 1 286789035 1 255653921 1 274310529 1 494521015 3 2 255653921 2 286789035 3 1 236295092 1 254828111 2 254828111 1 465995753 1 85886315 1 7959587 1 20842598 2 7959587 3 1 -51159108 3 2 -51159108 3 1 789534713
from heapq import heappush, heappop h= [] p = set()
data = [input().split(' ') for _ in range(int(input()))] for idx in range(len(data)): records = data[idx] if records[0] == '1': # p.add(records[1],heappush(h, records[1])) p.add(int(records[1])) heappush(h, int(records[1])) elif records[0] == '2': p.discard(int(records[1])) elif records[0] == '3': while h[0] not in p: heappop(h) print(h[0])
Here is hackerrank QHEAP1 problem solution in Python, Java, C++, C and javascript
In JAVA8 :