Data structures and algorithms (1) Spur task 8--find Median from Data Stream

Source: Internet
Author: User

The topics are as follows: (https://leetcode.com/problems/find-median-from-data-stream/)

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.

The topic needs to implement two functions: adding elements to an integer table and finding the median. Here are two ideas:

Idea One

If you use vectors to store integers, finding the median is easier (O (1)), adding integers can be time consuming, so try using a binary insert with a time complexity of O (log (n)).

1 classMedianfinder {2  Public:3     BOOLOdd//whether the length is odd. Odd is true, even is false4     intBegin, end, mid;//array header, tail, middle subscript5vector<int> L;//Array6 7Medianfinder (): Odd (false), Begin (0), End (0), Mid (0) {}8 9     //Add integerTen     voidAddnum (intnum) { One         //Update AOdd = odd?false:true; -Begin =0; -End = L.size ()-1; the         //Two-point search -          while(Begin <=end) -         { -MID = (begin + END)/2; +             if(L[mid] = =num) -             { +                  Break; A             } at             Else if(L[mid] >num) -             { -End = Mid-1; -             }  -             Else -             { inBegin = Mid +1; -             } to         } +         //Insert -         if(Begin >end) the         { *L.insert (L.begin () +begin, num); $         } Panax Notoginseng         Else -         { theL.insert (L.begin () +mid, num); +         } A     } the  +     //returns the median number -     DoubleFindmedian () { $Begin =0; $End = L.size ()-1; -MID = (begin + END)/2; -         if(Odd) the         { -             returnL[mid];Wuyi         }  the         Else -         { Wu             return((Double) L[mid] + l[mid+1]) /2; -         } About     } $};
View Code

Two ideas

The tip of the topic says to use the heap, so consider the priority queue in the STL. Data on both sides of the median can be stored to two priority queues, one integer with a high priority (default) (equivalent to a large top heap), and the other with a small priority (equivalent to a small, well-ordered top-level heap).

1 classMedianfinder {2  Public:3     BOOLOdd//whether the length is odd. Odd is true, even is false4priority_queue<int> front;//The first half of the array, the priority queue defaults to a higher priority5priority_queue<int, vector<int, greater<int>> back;//The second half of the array, with the smaller number having higher precedence6     7Medianfinder (): Odd (false) {}8 9     //Add integerTen     voidAddnum (intnum) { One         //Update the status of a length AOdd = odd?false:true; -         //Insert num and ensure that the front length is not less than the back length -         if(Odd) the         { -             if(Back.size () && num >back.top ()) -             { - Back.push (num); + Front.push (Back.top ()); - Back.pop (); +             }  A             Else at             { - Front.push (num); -             } -         }  -         Else -         { in             if(Front.size () && num >front.top ()) -             { to Back.push (num); +             }  -             Else the             { * Front.push (num); $ Back.push (Front.top ());Panax Notoginseng Front.pop (); -             } the         } +     } A  the     //returns the median number +     DoubleFindmedian () { -         if(Odd) $         { $             returnfront.top (); -         }  -         Else the         { -             return((Double) front.top () + back.top ())/2;Wuyi         } the     } -};
View Code

Report:

Leave a hole first, finish the test and then fill it ...

Data structures and algorithms (1) Spur task 8--find Median from Data 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.