-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
sliding-window-median.cpp
65 lines (59 loc) · 2.05 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
55
56
57
58
59
60
61
62
63
64
65
// Time: O(nlogk)
// Space: O(k)
class Solution {
public:
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
multiset<double> min_bst(cbegin(nums), cbegin(nums) + k);
auto mid = next(cbegin(min_bst), k / 2);
vector<double> result = {((*mid) + *prev(mid, 1 - k % 2)) / 2};
for (int i = k; i < size(nums); ++i) {
min_bst.emplace(nums[i]);
if (nums[i] < *mid) {
--mid;
}
if (nums[i - k] <= *mid) {
++mid;
}
min_bst.erase(min_bst.lower_bound(nums[i - k]));
result.emplace_back(((*mid) + *prev(mid, 1 - k % 2)) / 2);
}
return result;
}
};
// Time: O(nlogk)
// Space: O(k)
class Solution2 {
public:
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
multiset<int, less<int>> min_bst;
multiset<int, greater<int>> max_bst;
vector<double> result;
for (int i = 0; i < nums.size(); ++i) {
if (i >= k) {
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]));
}
}
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 (i >= k - 1) {
result.emplace_back(min_bst.size() == max_bst.size() ?
*max_bst.cbegin() / 2.0 + *min_bst.cbegin() / 2.0 : *min_bst.cbegin());
}
}
return result;
}
};