[Leetcode] Find Median from data stream finding the median of a stream

Source: Internet
Author: User

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. The median is the mean of the middle value.

Examples:

[2,3,4], the median is3

[2,3], the median is(2 + 3) / 2 = 2.5

Design a data structure that supports the following and the operations:

    • void addnum (int num)-Add A integer number from the data stream to the data structure.
    • Double Findmedian ()-Return The median of all elements so far.

For example:

Add (1) Add (2) Findmedian () 1.5add (3) Findmedian () 2

Credits:
Special thanks to @Louis1992 for adding this problem and creating all test cases.

This problem gives us a stream of data, let's find the median, because the data in the stream is not ordered, so we should first think of a way to make it orderly. If we use vectors to store data streams, it is not efficient to sort the arrays in each new data. So the thought of using multiset this data structure, is ordered to save information, but it can not use subscript direct access to elements, find the median number is not efficient. The solution used here is very ingenious, we use the size of the heap to solve the problem, where the large heap to save the right half of the larger number, a small heap to save the left half of the smaller array. So the whole array is divided into two sections, because the heap is kept in the way from large to small, we hope that the data inside the large heap is small to large, so that the first to calculate the median number of convenient. We use a little trick, that is, the number that is stored in a large heap is first reversed, so that the order from the big to the small is actually what we want in order from small to large. When the large heap and the number in the small heap as long, we take out a lot of small heap of the first element to average, when the small heap of elements, the first element of the small heap is the median, see the code is as follows:

Solution One:

classMedianfinder { Public:    //Adds A number into the data structure.    voidAddnum (intnum)        {Small.push (num); Large.push (-small.top ());        Small.pop (); if(Small.size () <large.size ()) {Small.push (-large.top ());        Large.pop (); }    }    //Returns The median of the current data stream    DoubleFindmedian () {returnSmall.size () > Large.size ()? Small.top ():0.5* (Small.top ()-large.top ()); }Private: Priority_queue<Long>small, Large;};

The above method uses priority_queue to implement the heap function, we can also use Multiset to implement the heap, see the code as follows:

Solution Two:

classMedianfinder { Public:    //Adds A number into the data structure.    voidAddnum (intnum)        {Small.insert (num); Large.insert (-*Small.begin ());        Small.erase (Small.begin ()); if(Small.size () <large.size ()) {Small.insert (-*Large.begin ());        Large.erase (Large.begin ()); }    }    //Returns The median of the current data stream    DoubleFindmedian () {returnSmall.size () > Large.size ()? *small.begin ():0.5* (*small.begin ()-*Large.begin ()); }Private: Multiset<Long>small, Large;};

Resources:

Https://leetcode.com/discuss/64850/short-simple-java-c-python-o-log-n-o-1

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Find Median from data stream finding the median of a stream

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.