Prove safety offer: median data stream (small + large top stack top stack)

1. Title Description

/ ** 
    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. 
* /

2. Ideas

  / ** The maximum and minimum heap stack 
      * top of each stack is inserted into a small large current top of the stack of the maximum number of 
      * Each stack top is inserted in the large number of the smallest minor vertex current stack 
      * This ensures that the top of the heap of small it is always greater than the number equal to the number of big top stack (value) 
      * median can be easily acquired from the root node of the two 
      * odd number of elements, then the stack is greater than the small root root large number of elements in the stack (the number of ) 
      * /

3. Code

Import java.util.PriorityQueue;
 Import java.util.Comparator;
 public  class Solution {
     // small stack top 
    Private PriorityQueue <Integer> = minheap new new PriorityQueue <Integer> ();
     // large stack top 
    Private PriorityQueue <Integer> = maxheap new new PriorityQueue <Integer> ( new new Comparator <Integer> () { 
        @Override 
        public  int Compare (O1 Integer, Integer O2) {
             return O2 - O1; 
        } 
    }); 
     
    // record the number of odd or even number of elements 
    int0 = COUNT ; 
    public  void the Insert (Integer NUM) {
         / ** an even number, then the top of the stack into the large first, then the top of the stack a large number of small insertion maximum top stack * / 
        // guarantees first placed in a small heap root, the root of the heap into a large 
        IF (COUNT% 2 == 0 ) { 
            maxHeap.offer (NUM); 
            int max = maxHeap.poll (); 
            minHeap.offer (max); 
        } the else {
             / ** an odd number, then the top of the first stack is inserted into a small, then the top of the small stack top inserted into the large number of the smallest stack * / 
            minHeap.offer (NUM); 
            int min = minHeap.poll (); 
            maxHeap.offer ( min); 
        } 
        / ** insert a, an increased number of * / 
        COUNT++ ; 
    } 
    public Double GetMedian () {
         IF (COUNT% 2 == 0 ) {
             / ** this is an even number, then take a small roof top of the stack and heap big top stack element averaging * / 
            return  new new Double (minheap .peek () + maxHeap.peek ()) / 2 ; 
        } the else {
             / ** this is an odd number, directly from small roof element can take the stack * / 
            return  new new Double (minHeap.peek ()); 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/haimishasha/p/11520748.html