import heapq
class MedianFinder:
def __init__(self):
min_heap = []
max_heap = []
heapq.heapify(min_heap)
heapq.heapify(max_heap)
self.min_heap = min_heap
self.max_heap = max_heap
def addNum(self, num: int) -> None:
heapq.heappush(self.min_heap, num)
heapq.heappush(self.max_heap, -heapq.heappop(self.min_heap))
if len(self.min_heap) < len(self.max_heap):
heapq.heappush(self.min_heap, -heapq.heappop(self.max_heap))
def findMedian(self) -> float:
if len(self.min_heap) > len(self.max_heap):
return self.min_heap[0]
else:
return (self.min_heap[0] + (-self.max_heap[0]) ) / 2