LeetCode – Find Median from Data Stream (Java)

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

Analysis

First of all, it seems that the best time complexity we can get for this problem is O(log(n)) of add() and O(1) of getMedian(). This data structure seems highly likely to be a tree.

We can use heap to solve this problem. In Java, the PriorityQueue class is a priority heap. We can use two heaps to store the lower half and the higher half of the data stream. The size of the two heaps differs at most 1.

find-median-from-stream

Java Solution

class MedianFinder {
    PriorityQueue<Integer> maxHeap;//lower half
    PriorityQueue<Integer> minHeap;//higher half
 
    public MedianFinder(){
        maxHeap = new PriorityQueue<Integer>(Collections.reverseOrder());
        minHeap = new PriorityQueue<Integer>();
    }
 
    // Adds a number into the data structure.
    public void addNum(int num) {
        maxHeap.offer(num);
        minHeap.offer(maxHeap.poll());
 
        if(maxHeap.size() < minHeap.size()){
            maxHeap.offer(minHeap.poll());
        }
    }
 
    // Returns the median of current data stream
    public double findMedian() {
        if(maxHeap.size()==minHeap.size()){
            return (double)(maxHeap.peek()+(minHeap.peek()))/2;
        }else{
            return maxHeap.peek();
        }
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Sonali Vishwakarma

    Very similar to the above solution, but much more intuitive. It actually also ran faster.

    class MedianFinder {
    PriorityQueue maxHeap;//lower half
    PriorityQueue minHeap;//higher half

    public MedianFinder(){
    // Basically Collections.reverseOrder() makes your PriorityQueue a “maxHeap”. Otherwise, its a minHeap by default.
    maxHeap = new PriorityQueue(Collections.reverseOrder());
    minHeap = new PriorityQueue();
    }

    // Adds a number into the data structure.
    public void addNum(int num) {
    double median = 0;

    // If there are no elements in maxHeap or minHeap, num is the median
    if (maxHeap.size() == 0 && minHeap.size() == 0) {
    median = (double) num;
    }
    else {
    median = findMedian();
    }

    // Balancing minHeap and maxHeap. They should either have equal number of elements or differ by a size of one.

    // If num is less than or equal to median, we should be putting it in maxHeap.
    if (num <= median) {
    // If maxHeap size is lesser than or equal to minHeap size, put it in maxHeap.
    if (maxHeap.size() <= minHeap.size()) {
    maxHeap.offer(num);
    }
    // If maxHeap size is greater than minHeap size, make some room in maxHeap,
    // by removing the root from maxHeap and putting it in minHeap. Then,
    // adding the num to maxHeap, since we made some room in the maxHeap.
    else {
    minHeap.offer(maxHeap.poll());
    maxHeap.offer(num);
    }
    }
    // If num is greater than median, we should be putting it in minHeap.
    else {
    // If minHeap size is lesser than or equal to maxHeap size, put it in minHeap.
    if (minHeap.size() minHeap.size()) {
    return (double) (maxHeap.peek());
    } else {
    return (double) (minHeap.peek());
    }
    }
    }

  • anil

    can someone xplain the logic behind this i mean how priority queue is used

  • Utkarsh Tiwari

    I love this website 🙏🏻