-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathdata-stream-median.cpp
77 lines (67 loc) · 2.33 KB
/
data-stream-median.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
// Time: O(nlogn)
// Space: O(n)
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: The median of numbers
*/
vector<int> medianII(vector<int> &nums) {
// 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;
vector<int> ans;
for (const auto& num : nums) {
// 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());
}
}
ans.emplace_back(min_bst.size() == max_bst.size() ?
*max_bst.cbegin() : *min_bst.cbegin());
}
return ans;
}
};
class Solution2 {
public:
/**
* @param nums: A list of integers.
* @return: The median of numbers
*/
vector<int> medianII(vector<int> &nums) {
// 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;
vector<int> ans;
for (const auto& num : nums) {
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();
}
}
ans.emplace_back(min_heap.size() == max_heap.size() ?
max_heap.top() : min_heap.top());
}
return ans;
}
};