题目标题

数据流求中位数、最大值、最小值

难度:中级

算法 数据结构
参考解析
  1. class MedianFinder:
  2. def __init__(self):
  3. """
  4. initialize your data structure here.
  5. """
  6. self.minheap = []
  7. self.maxheap = []
  8. def addNum(self, num: int) -> None:
  9. heappush(self.minheap, -heappushpop(self.maxheap, -num))
  10. if len(self.minheap) > len(self.maxheap):
  11. heappush(self.maxheap, -heappop(self.minheap))
  12. def findMedian(self) -> float:
  13. if len(self.maxheap) > len(self.minheap):
  14. return -self.maxheap[0]
  15. return (self.minheap[0] - self.maxheap[0]) / 2