The median in the offline data stream and the median in the data stream

Source: Internet
Author: User

The median in the offline data stream and the median in the data stream

[Disclaimer: All Rights Reserved. indicate the source for reprinting. Do not use it for commercial purposes. Contact mailbox: libin493073668@sina.com]


Question link: http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1? Rp = 4 & ru =/ta/coding-interviews & qru =/ta/coding-interviews/question-ranking


Description
How to obtain the median in a data stream? If an odd number is read from the data stream, the median is the value in the middle after all values are sorted. If an even number is read from the data stream, the median is the average value of the two numbers in the middle after all values are sorted.

Ideas
We divide all input into two heaps. The first half of the minimum Heap Storage is a relatively small number, and the last half of the maximum Heap Storage is a relatively large number.
To ensure the average data distribution, we need to make the difference between the two heaps of data no more than 1. Therefore, when the total number is an even number, we need to insert the new data into the minimum heap, otherwise, insert the largest heap.
At the same time, we also need to ensure that all elements in the max heap are smaller than the min heap. When the length is even, we aim to insert the min heap, if the maximum element of the Max heap is greater than the element to be inserted at this time, we will take out the element of the Max heap and replace it with the insert element. Then, we will insert the pop-up element into the min heap.
The same applies when the length is odd.


class Solution{public:void Insert(int num){int len = min.size()+max.size();if(len%2 == 0){if(max.size()>0 && max[0]>num){max.push_back(num);push_heap(max.begin(),max.end(),less<int>());num = max[0];pop_heap(max.begin(),max.end(),less<int>());max.pop_back();}min.push_back(num);push_heap(min.begin(),min.end(),greater<int>());}else{if(min.size()>0 && min[0]<num){min.push_back(num);push_heap(min.begin(),min.end(),greater<int>());num = min[0];pop_heap(min.begin(),min.end(),greater<int>());min.pop_back();}max.push_back(num);push_heap(max.begin(),max.end(),less<int>());}}double GetMedian(){int len = min.size()+max.size();if(len==0)return 0;double median = 0;if(len&1)median = min[0];elsemedian = (double)(min[0]+max[0])/2;return median;}private:vector<int> min;vector<int> max;};


Copyright Disclaimer: This article is the original article of the blogger. If it is reproduced, please indicate the source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.