forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-median-from-data-stream.cpp
81 lines (70 loc) · 2.42 KB
/
find-median-from-data-stream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Time: O(nlogn) for total n addNums, O(logn) per addNum, O(1) per findMedian.
// Space: O(n), total space
// Heap solution.
class MedianFinder {
public:
// Adds a number into the data structure.
void addNum(int num) {
// Balance smaller half and larger half.
if (max_heap_.empty() || num > max_heap_.top()) {
min_heap_.emplace(num);
if (min_heap_.size() > max_heap_.size() + 1) {
max_heap_.emplace(min_heap_.top());
min_heap_.pop();
}
} else {
max_heap_.emplace(num);
if (max_heap_.size() > min_heap_.size()) {
min_heap_.emplace(max_heap_.top());
max_heap_.pop();
}
}
}
// Returns the median of current data stream
double findMedian() {
return min_heap_.size() == max_heap_.size() ?
(max_heap_.top() + min_heap_.top()) / 2.0 :
min_heap_.top();
}
private:
// min_heap_ stores the larger half seen so far.
priority_queue<int, vector<int>, greater<int>> min_heap_;
// max_heap_ stores the smaller half seen so far.
priority_queue<int, vector<int>, less<int>> max_heap_;
};
// BST solution.
class MedianFinder2 {
public:
// Adds a number into the data structure.
void addNum(int num) {
// Balance smaller half and larger half.
if (max_bst_.empty() || num > *max_bst_.cbegin()) {
min_bst_.emplace(num);
if (min_bst_.size() > max_bst_.size() + 1) {
max_bst_.emplace(*min_bst_.cbegin());
min_bst_.erase(min_bst_.cbegin());
}
} else {
max_bst_.emplace(num);
if (max_bst_.size() > min_bst_.size()) {
min_bst_.emplace(*max_bst_.cbegin());
max_bst_.erase(max_bst_.cbegin());
}
}
}
// Returns the median of current data stream
double findMedian() {
return min_bst_.size() == max_bst_.size() ?
(*max_bst_.cbegin() + *min_bst_.cbegin()) / 2.0 :
*min_bst_.cbegin();
}
private:
// min_bst_ stores the larger half seen so far.
multiset<int, less<int>> min_bst_;
// max_bst_ stores the smaller half seen so far.
multiset<int, greater<int>> max_bst_;
};
// Your MedianFinder object will be instantiated and called as such:
// MedianFinder mf;
// mf.addNum(1);
// mf.findMedian();