C++     Data Stream     Design     Hard     Heap (Priority Queue)     Sorting     Two Pointers    

Problem Statement:

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.

  • For example, for arr = [2,3,4], the median is 3.
  • For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.

Implement the MedianFinder class:

  • MedianFinder() initializes the MedianFinder object.
  • void addNum(int num) adds the integer num from the data stream to the data structure.
  • double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.

 

Example 1:

Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]

Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1);    // arr = [1]
medianFinder.addNum(2);    // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3);    // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0

 

Constraints:

  • -105 <= num <= 105
  • There will be at least one element in the data structure before calling findMedian.
  • At most 5 * 104 calls will be made to addNum and findMedian.

 

Follow up:

  • If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
  • If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?

Solution:

This is a classical heaps question. The solution is to maintain two heaps, a max heap of lower than median values and a min heap of higher than median values. On querying for median, you return from the top of whichever heap as an extra element or in case both have same number of elements (size of array is even), then avarage of top of both heaps.

 
class MedianFinder {
public:
    priority_queue<int> q1; //lower half is a max heap
    priority_queue<int, vector<int>, greater<int>> q2; // upper half is a min heap
    MedianFinder() {        
    }
    
    void addNum(int num) {
        if (q1.size()==0 || num < q1.top()) q1.push(num);
        else q2.push(num);
        if (q1.size() > q2.size()+1)
        {
            q2.push(q1.top());
            q1.pop();
        } else if (q2.size() > q1.size()+1)
        {
            q1.push(q2.top());
            q2.pop();
        }
    }
    
    double findMedian() {
        int n1=q1.size(), n2=q2.size();
        if (n1 > n2) return q1.top();
        else if (n2 > n1) return q2.top();
        else if (n1==n2) return .5 * (double)q1.top() + .5 * (double)q2.top();
        return -1;
    }
};