Wins the Offer-63. The median data stream (C ++ / Java)

topic:

How to get the median of a data stream? If the numerical value is read from the odd data stream, then the median value is located in the middle of all values ​​after sorting. If an even number value is read out from the data stream, then the median is the average of two numbers after all intermediate values ​​of order. We use the Insert () method to read the data stream, using GetMedian () method to get the current median data is read.

analysis:

We imagine a sorted array may be an array of bits divided into two parts, then we use the minimum and maximum heap stack to store the two parts separately, and the median if required, can be returned directly top of the stack elements, in Solution (1) in a time O.

To ensure that all elements of the maximum heap less the minimum stack element, the insertion element has been inserted, when judged by the number of digits:

If the number is even inserted, the insertion element will be the maximum stack, then the maximum heap stack top element removed, insert the minimum stack.

If the insert is an odd number, the elements will be inserted into the min-heap, then the minimum heap stack top element removed, insert the largest stack.

Doing so ensures maximum heap all the elements is less than equal to the smallest element of the heap.

Then if the number is an odd number, the top element in the stack is equal to the median of the minimum heap, is an even number, equal to the average median of the minimum and the top of the heap stack elements the maximum stack.

To [5,2,3,4,1,6,7,0,8] as an example:

Insert elements The minimum heap The maximum heap Median
5 [5] [] 5
2 [5] [2] 3.5
3 [3,5] [2] 3
4 [4,5] [3,2] 3.5
1 [3,4,5] [2,1] 3
6 [4,5,6] [3,2,1] 3.5
7 [4,5,6,7] [3,2,1] 4
0 [4,5,6,7] [3,2,1,0] 3.5
8 [4,5,6,7,8] [3,2,1,0] 4

 

program:

C++

class Solution {
public:
    void Insert(int num)
    {
        if(c % 2 == 0){
            maxHeap.push(num);
            minHeap.push(maxHeap.top());
            maxHeap.pop();
        }
        else{
            minHeap.push(num);
            maxHeap.push(minHeap.top());
            minHeap.pop();
        }
        c++;
    }

    double GetMedian()
    { 
        if(c % 2 == 0){
            return (double)(maxHeap.top() + minHeap.top()) / 2;
        }
        else{
            return (double)minHeap.top();
        }
    }
private:
    priority_queue<int, vector<int>, less<int> > maxHeap;
    priority_queue<int, vector<int>, greater<int> > minHeap;
    int c = 0;
};

Java

import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {

    public void Insert(Integer num) {
        if(count % 2 == 0){
            maxHeap.offer(num);
            minHeap.offer(maxHeap.poll());
        }else{
            minHeap.offer(num);
            maxHeap.offer(minHeap.poll());
        }
        count++;
    }

    public Double GetMedian() {
        if(count % 2 == 0){
            return newDouble (minHeap.peek () + maxHeap.peek ()) / 2 ; 
        } Else {
             return  new Double (minHeap.peek ()); 
        } 
    } 
    Private PriorityQueue <Integer> = minHeap new PriorityQueue <Integer> ();
    private PriorityQueue <Integer> = maxHeap new PriorityQueue <Integer> (11, new Comparator <Integer> () { 
        @Override 
        public  int compare (o1 Integer, Integer o2) {
             return o2 - o1; 
        } 
    }); 
    private  int count = 0;

}

Guess you like

Origin www.cnblogs.com/silentteller/p/12119109.html