input
operations
operations.len ≤ 1,000,000output
O(nlogn)import heapq
def solution(operations):
min_heap = []
max_heap = []
heapq.heapify(min_heap)
heapq.heapify(max_heap)
for operation in operations:
operation = operation.split()
work = operation[0]
num = int(operation[1])
if work == 'I':
heapq.heappush(min_heap, num)
heapq.heappush(max_heap, -num)
else:
if max_heap and min_heap:
if num == 1:
max_num = heapq.heappop(max_heap)
min_heap.remove(-max_num)
else:
min_num = heapq.heappop(min_heap)
max_heap.remove(-min_num)
return [0, 0] if not max_heap and not min_heap else [-(heapq.heappop(max_heap)), heapq.heappop(min_heap)]