Median data stream OJ_4_

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.

 

Ideas:

The median reference can link the data stream: https://www.jianshu.com/p/f7e2ed52052d

 

. 1 #include <the iostream>
 2 #include <Vector>
 . 3 #include <Queue>
 . 4  
. 5  the using  namespace STD;
 . 6  
. 7  // median data stream 
. 8  class Solution {
 . 9  Private :
 10      int COUNT = 0 ;
 . 11      The priority_queue < int , Vector < int >> max_heap;   // maximum stack 
12 is      The priority_queue < int , Vector < int >, Greater < int >> min_heap;   // minimum heap 
13 
14  public :
 15      void the Insert ( int NUM)
 16      {
 . 17          COUNT ++ ;
 18 is          // even-numbered -> Insert minimum heap; 
. 19          IF ((& COUNT . 1 ) == 0 ) {    // even-even-efficient writing determination 
20 is              min_heap .push (NUM);
 21 is              IF (! max_heap.empty () && max_heap.top ()> NUM)    // If a new element is to be inserted in the top of the stack than the stack is smaller maximum 
22 is              {
 23 is                  min_heap.pop ();
 24  
25                  int TEMP = max_heap.top ();
 26 is                 max_heap.pop ();
 27                  min_heap.push (TEMP);     // maximum stack top of the stack is inserted into a minimum heap 
28  
29                  max_heap.push (NUM);      // new element into the maximum stack 
30              }
 31          }
 32          // odd-numbered -> insert maximum heap; 
33 is          the else {
 34 is              max_heap.push (NUM);
 35              IF (min_heap.empty () && min_heap.top () <NUM!) {    // If a new element to be inserted in the top of the stack than the minimum stack larger 
36                  max_heap.pop ();
 37 [  
38 is                  int TEMP = min_heap.top ();
 39                 min_heap.pop ();
 40                  max_heap.push (TEMP);     // smallest maximum stack top of the stack is inserted into the stack 
41 is  
42 is                  min_heap.push (NUM);      // new element into a minimum heap 
43              }
 44          }
 45          // above manner the median can be maintained minimum and maximum stack top of the stack in the stack, if the number is even, the average was two piles top, if an odd number, compared with the maximum top piles; 
46 is      }
 47  
48      Double GetMedian ()
 49      {
 50          Double RES;
 51 is          IF ((& COUNT . 1 ) == 0 ) {   // even number 
52 is              RES = ((double)max_heap.top()+(double)min_heap.top())/2;
53         }
54         else{
55             res = max_heap.top();
56         }
57 
58         return res;
59     }
60 
61 };
62 
63 int main()
64 {
65     Solution sol;
66     vector<int> nums={5,2,3,4,1,6,7,0,8,5};
67     for(int i =0; i<nums.size();i++){
68         sol.Insert(nums[i]);
69     }
70     cout<<sol.GetMedian()<<endl;
71 }

 

Guess you like

Origin www.cnblogs.com/grooovvve/p/12367724.html