-
Notifications
You must be signed in to change notification settings - Fork 258
/
sliding-window-median.cpp
55 lines (48 loc) · 1.75 KB
/
sliding-window-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
// Time: O(nlogw)
// Space: O(w)
#include <vector>
#include <set>
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: The median of the element inside the window at each moving
*/
vector<int> medianSlidingWindow(vector<int> &nums, int k) {
// 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 (int i = 0; i < nums.size(); ++i) {
if (i >= k) {
// Remove the element outside the window.
if (max_bst.find(nums[i - k]) != max_bst.cend()) {
max_bst.erase(max_bst.find(nums[i - k]));
} else {
min_bst.erase(min_bst.find(nums[i - k]));
}
}
// Balance smaller half and larger half.
if (max_bst.empty() || nums[i] > *max_bst.cbegin()) {
min_bst.emplace(nums[i]);
if (min_bst.size() > max_bst.size() + 1) {
max_bst.emplace(*min_bst.cbegin());
min_bst.erase(min_bst.cbegin());
}
} else {
max_bst.emplace(nums[i]);
if (max_bst.size() > min_bst.size()) {
min_bst.emplace(*max_bst.cbegin());
max_bst.erase(max_bst.cbegin());
}
}
// If window is full, get the median from 2 BST.
if (i >= k - 1) {
ans.emplace_back(min_bst.size() == max_bst.size() ?
*max_bst.cbegin() : *min_bst.cbegin());
}
}
return ans;
}
};