The median is the middle value in an ordered integer list. If the list size is even, the median is the mean of the two middle values.
Implement MedianFinder:
addNum(int num) adds an integer from the data stream,
findMedian() returns the median of all elements so far.
addNum(1), addNum(2), findMedian(), addNum(3), findMedian()[1.5, 2.0]addNum(6), addNum(3), findMedian(), addNum(7), addNum(2), findMedian()[4.5, 4.5]maxH = max-heap (lower half), minH = min-heap (upper half)num to maxH, then push maxH.poll() to minHminH.size > maxH.size, move minH.poll() to maxHfindMedian: if maxH.size > minH.size → maxH.peek(); else → avg of topsWe keep the stream split at the median boundary: maxH (max-heap) holds the lower half so its top is the largest small value, and minH (min-heap) holds the upper half so its top is the smallest large value. By ensuring sizes stay balanced (equal or maxH one larger), the median is always instantly available at the heap tops — no scanning needed. Each addNum is O(log n), findMedian is O(1).