Median prove safety offer 63. tree data stream

 

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.

Problem-solving ideas

  • Java first with a small set of set top PriorityQueue stack and heap big top
  • The main idea is: because the median is required, then the two reactors, the big top of the heap used to keep a small number, in descending order ;
  • Top stockpiling large number of small, from small to large order sorting * , apparently median is the root and the root of the heap small top big top of the heap and average.
  • ⭐ ensure: a small top stack elements are greater than or equal big top element in the stack, the value of each plug, not directly go into, but rather a maximum (minimum) value of a plug from another stack poll
  • ⭐ When an even number of times, this value is inserted into the large top of the stack, and then a large heap root top (i.e. maximum value) is inserted into the top of a small stack;
  • ⭐ when the number is odd, when this value is inserted into the top of the small stack, the stack top repeat small root node (i.e., minimum) is inserted into a large pile top;
  • ⭐ median time taken if the current number is even, the top stack obviously take a small and a large average top stack root node; if the current number is odd, obviously take a small top stack root

I appreciated that the main idea of ​​the above, the following example to test the auxiliary.

For example, incoming data is: [5,2,3,4,1,6,7,0,8], then as required, the output is "5.00 3.50 3.00 3.50 3.00 3.50 4.00 3.50 4.00"

Then the execution flow of the entire process should be (represented by a small pile top min, max indicates a large stack top):

  • 5 to enter a large stack top, then the top of the large maximum stack into a small pile top, this time min = [5], max = [no], avg = [5.00]
  • 2 to enter the small pile top, the top of the stack and a small minimum value into a large pile top, this time min = [5], max = [2], avg = [(5 + 2) / 2] = [3.50 ]
  • 3 to enter the top of the heap large, then a large maximum stack top into a small pile top, this time min = [3,5], max = [2], avg = [3.00]
  • 4 to enter the small pile top, the top of the stack and a small minimum value into a large pile top, this time min = [4,5], max = [3,2], avg = [(4 + 3) / 2 ] = [3.50]
  • 1 into the first large stack top, then the top of the large maximum stack into a small pile top, this time min = [3,4,5], max = [2,1], avg = [3/00]
  • 6 to enter the small pile top, the top of the stack and a small minimum value into a large pile top, this time min = [4,5,6], max = [3,2,1], avg = [(4+ 3) / 2] = [3.50]
  • 7 to enter the top of the heap large, large and small roof top of the stack into the stack the maximum, at this time min = [4,5,6,7], max = [3,2,1], avg = [4 ] = [4.00]
  • 0 to enter the small pile top, the top of the stack and a small maximum value into a small pile top, this time min = [4,5,6,7], max = [3,2,1,0], avg = [(4 + 3) / 2] = [3.50]
  • 8 to enter the top of the heap large, then a large minimum top pile into a large pile top, this time min = [4,5,6,7,8], max = [3,2,1,0], avg = [4.00]  

 code show as below

            @ Small stack top
             Private PriorityQueue <Integer> = minheap new new PriorityQueue <Integer> (); 
             
            // large stack top 
            Private PriorityQueue <Integer> = maxheap new new PriorityQueue <Integer> (15, new new Comparator <Integer> () { 
                @Override 
                public  int Compare (O1 Integer, Integer O2) {
                     return O2 - O1; 
                } 
            }); 
             
            // record an even number or an odd number 
            int COUNT = 0 ;
             // every time the top of stack is inserted into a small large current maximum stack top number
             //Each stack is inserted into the top of large number of small current minimum top stack
             @ This ensures that a small number of the top of the stack is always greater than or equal to a large number of the top stack
             // median can be conveniently prepared from the root of both point acquired 
            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 stack top 
                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 inserted into the large number of the smallest stack top 
                    minHeap.offer (NUM);
                     int min = minHeap.poll ();
                    maxHeap.offer (min); 
                } 
                COUNT ++ ; 
            } 
            public Double GetMedian () {
                 // this is an even number, then take a small roof top of the stack and heap big top stack elements averaged 
                IF (COUNT% 2 == 0 ) {
                     return  new new Double (minHeap.peek () + maxHeap.peek ()) / 2 ; 
                } the else {
                     // this is an odd number, the top of the stack taken directly from small elements to 
                    return  new new Double (minHeap.peek ()) ; 
                } 
            }

 

 

Guess you like

Origin www.cnblogs.com/Transkai/p/11422891.html