Problem
Numbers arrive from a stream, a number at a time.
The median is the middle value of the numbers in sorted order; with an even count, it is the average of the two middle values.
addNum adds a number from the stream.
findMedian returns the median of all the numbers added so far.
Examples
Example 1:
Input: calls = ["MedianFinder","addNum","addNum","findMedian","addNum","findMedian"], args = [[],[1],[2],[],[3],[]]
Output: [null,null,null,1.5,null,2.0]
Explanation: each test makes a MedianFinder and then the calls, in order, each with its arguments: addNum(1), addNum(2), findMedian(), addNum(3), findMedian(). Output has an answer for every call, null where a call returns nothing: the median of 1 and 2 is 1.5, of 1, 2 and 3 it is 2.0.
Example 2:
Input: calls = ["MedianFinder","addNum","findMedian","addNum","findMedian","addNum","findMedian","addNum","findMedian","addNum","findMedian","addNum","findMedian"], args = [[],[5],[],[2],[],[8],[],[4],[],[7],[],[3],[]]
Output: [null,null,5.0,null,3.5,null,5.0,null,4.5,null,5.0,null,4.5]
Explanation: the stream 5, 2, 8, 4, 7, 3, with the median after every number.
Constraints
-10^5 <= num <= 10^5
findMedian is called only after at least one number has been added.
At most 5 * 10^4 calls are made to addNum and findMedian.
An answer within 10^-5 of the median is accepted.
How to think about it
1. Two Heaps Facing Each Other Optimal
Intuition
Keep the smaller half of the numbers in a max-heap and the larger half in a min-heap. If the smaller half holds as many as the larger or one more, and nothing in it is bigger than anything in the larger, the median is at the tops: the smaller half's top when the count is odd, the average of the two tops when it is even. Every number goes into the smaller half and its biggest moves over to the larger, so the order between the halves holds; when the larger half then holds more, its smallest moves back, so the sizes hold.
Algorithm
1. small is a max-heap for the smaller half (Python's heapq holds it negated; Java's PriorityQueue takes Collections.reverseOrder()), large a min-heap for the larger half.
2. addNum: push the number into small, then move small's biggest into large.
3. If large now holds more than small, move large's smallest back into small.
4. findMedian: small's top if small holds more, else the two tops added and divided by 2.
Time & Space
Time O(log n) for addNum: three heap operations at most. O(1) for findMedian: two tops read. Space O(n): every number is in one of the heaps.
2. Compare First, Then Even the Sizes
Intuition
The same two heaps, filled the other common way. Instead of sending every number through the smaller half, a number is compared with the smaller half's biggest first: no bigger, it belongs to the smaller half; bigger, to the larger. The order between the halves holds by that comparison, and only the sizes can drift, so afterwards one number moves across in whichever direction they are off. The first approach needs no comparison and always moves the smaller half's biggest; this one compares and moves only when the sizes ask for it.
Algorithm
1. small is a max-heap for the smaller half (Python's heapq holds it negated; Java's PriorityQueue takes Collections.reverseOrder()), large a min-heap for the larger half.
2. addNum: if small is not empty and the number is bigger than small's top, push it into large; otherwise into small.
3. If small now holds two more than large, move small's biggest into large; if large holds more than small, move large's smallest into small.
4. findMedian: small's top if small holds more, else the two tops added and divided by 2.
Time & Space
Time O(log n) for addNum: two or three heap operations. O(1) for findMedian: two tops read. Space O(n): every number is in one of the heaps.