You are viewing a single comment's thread. Return to all comments →
PYTHON3 SOLUTION:
import sys import heapq class Heap: def __init__(self): self.heap = [] self.counts = {} def insert(self, val): heapq.heappush(self.heap, val) self.counts[val] = self.counts.get(val, 0) + 1 def delete(self, val): if self.counts.get(val, 0) > 0: self.counts[val] -= 1 def minimum(self): while self.heap and self.counts.get(self.heap[0], 0) == 0: heapq.heappop(self.heap) return self.heap[0] if self.heap else None def main(): q = int(sys.stdin.readline()) heap = Heap() out = [] for _ in range(q): parts = sys.stdin.readline().split() if parts[0] == "1": heap.insert(int(parts[1])) elif parts[0] == "2": heap.delete(int(parts[1])) else: out.append(str(heap.minimum())) print("\n".join(out)) if __name__ == "__main__": main()
Seems like cookies are disabled on this browser, please enable them to open this website
QHEAP1
You are viewing a single comment's thread. Return to all comments →
PYTHON3 SOLUTION: