-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path239.cpp
More file actions
38 lines (35 loc) · 774 Bytes
/
Copy path239.cpp
File metadata and controls
38 lines (35 loc) · 774 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
vector<int> maxSlidingWindow(vector<int> &nums, int k) {
vector<int> rst;
priority_queue<pair<int, int>> q;
for (auto i = 0; i < k - 1; ++i) {
q.emplace(nums[i], i);
}
for (auto i = k - 1; i < nums.size(); ++i) {
q.emplace(nums[i], i);
while (true) {
auto [num, index] = q.top();
if (index <= i - k) {
q.pop();
} else {
break;
}
}
rst.push_back(q.top().first);
}
return rst;
}
};
int main() {
Solution s;
vector<int> v = {9,10,9,-7,-4,-8,2,-6};
auto rst = s.maxSlidingWindow(v, 5);
for (auto i = 0; i < rst.size(); ++i) {
cout << rst[i] << " ";
}
cout << endl;
return 0;
}