-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
65 lines (59 loc) · 1.75 KB
/
solution.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
/**
* 42 / 42 test cases passed.
* Runtime: 32 ms
* Memory Usage: 14.1 MB
*/
class Solution {
public:
priority_queue<int> small;
priority_queue<int, vector<int>, greater<int>> big;
unordered_map<int, int> rec;
double get(int &k) {
if (k & 1) return small.top();
return small.top() * 0.5 + big.top() * 0.5;
}
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
int n = nums.size();
for (int i = 0; i < k; ++ i) small.push(nums[i]);
for (int i = 0, k2 = k / 2; i < k2; ++ i) {
big.push(small.top());
small.pop();
}
vector<double> ans{ get(k) };
for (int i = k; i < n; ++ i) {
int balance = 0; // size(small) - size(big)
int left = nums[i - k];
int right = nums[i];
++ rec[left];
if (!small.empty() && left <= small.top()) {
-- balance;
} else {
++ balance;
}
if (!small.empty() && right <= small.top()) {
++ balance;
small.push(right);
} else {
-- balance;
big.push(right);
}
if (balance > 0) {
big.push(small.top());
small.pop();
} else if (balance < 0) {
small.push(big.top());
big.pop();
}
while (!small.empty() && rec[small.top()] > 0) {
-- rec[small.top()];
small.pop();
}
while (!big.empty() && rec[big.top()] > 0) {
-- rec[big.top()];
big.pop();
}
ans.push_back(get(k));
}
return ans;
}
};