From fa2df8233a56e497430715f5cbf6a40bddec60ce Mon Sep 17 00:00:00 2001 From: Hao Chen Date: Sat, 14 Nov 2015 17:34:35 +0800 Subject: [PATCH] New Problem "Find Median from Data Stream" --- README.md | 1 + .../FindMedianFromDataStream.cpp | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp diff --git a/README.md b/README.md index e1bd3a8..3bcbe89 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ LeetCode |300|[Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) | [C++](./algorithms/cpp/longestIncreasingSubsequence/longestIncreasingSubsequence.cpp)|Medium| |299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [C++](./algorithms/cpp/bullsAndCows/bullsAndCows.cpp)|Easy| |297|[Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [C++](./algorithms/cpp/serializeAndDeserializeBinaryTree/SerializeAndDeserializeBinaryTree.cpp)|Medium| +|295|[Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [C++](./algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp)|Hard| |292|[Nim Game](https://leetcode.com/problems/nim-game/) | [C++](./algorithms/cpp/nimGame/nimGame.cpp)|Easy| |290|[Word Pattern](https://leetcode.com/problems/word-pattern/) | [C++](./algorithms/cpp/wordPattern/WordPattern.cpp)|Easy| |287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [C++](./algorithms/cpp/findTheDuplicateNumber/findTheDuplicateNumber.cpp)|Hard| diff --git a/algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp b/algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp new file mode 100644 index 0000000..567ac32 --- /dev/null +++ b/algorithms/cpp/findMedianFromDataStream/FindMedianFromDataStream.cpp @@ -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 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();