forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Problem "Find Median from Data Stream"
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Source : https://leetcode.com/problems/find-median-from-data-stream/ | ||
// Author : Hao Chen | ||
// Date : 2015-11-14 | ||
|
||
/*************************************************************************************** | ||
* | ||
* Median is the middle value in an ordered integer list. If the size of the list is | ||
* even, there is no middle value. So the median is the mean of the two middle value. | ||
* Examples: | ||
* [2,3,4] , the median is 3 | ||
* [2,3], the median is (2 + 3) / 2 = 2.5 | ||
* | ||
* Design a data structure that supports the following two 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.5 | ||
* add(3) | ||
* findMedian() -> 2 | ||
* | ||
* Credits:Special thanks to @Louis1992 for adding this problem and creating all test | ||
* cases. | ||
* | ||
***************************************************************************************/ | ||
|
||
|
||
class MedianFinder { | ||
|
||
private: | ||
//we seprate the sorted array to two parts | ||
multiset<int> first, second; | ||
|
||
public: | ||
|
||
// Adds a number into the data structure. | ||
void addNum(int num) { | ||
if (first.empty() || num <= *(first.rbegin()) ) { | ||
first.insert(num); | ||
} else { | ||
second.insert(num); | ||
} | ||
|
||
if (first.size() > second.size() + 1) { | ||
auto it = first.end(); | ||
it--; | ||
second.insert(*(it)); | ||
first.erase(it); | ||
} | ||
|
||
if ( first.size() < second.size() ) { | ||
first.insert(*(second.begin())); | ||
second.erase(second.begin()); | ||
} | ||
} | ||
|
||
// Returns the median of current data stream | ||
double findMedian() { | ||
if (first.size()> second.size()) { | ||
return *(first.rbegin()); | ||
} | ||
double x = *first.rbegin(); | ||
double y = *second.begin(); | ||
return (x+y)/2; | ||
} | ||
}; | ||
|
||
// Your MedianFinder object will be instantiated and called as such: | ||
// MedianFinder mf; | ||
// mf.addNum(1); | ||
// mf.findMedian(); |