-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path239.cpp
More file actions
39 lines (34 loc) · 764 Bytes
/
239.cpp
File metadata and controls
39 lines (34 loc) · 764 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
39
#include <deque>
#include <iostream>
#include <vector>
#include "common.h"
using namespace std;
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
vector<int> ret;
int left = 0;
int right = 0;
deque<int> q;
for (int i = 0; i < nums.size(); ++i) {
while (!q.empty() && nums[q.back()] < nums[i]) {
q.pop_back();
}
q.push_back(i);
if (i - q.front() + 1 > k) {
q.pop_front();
}
if ((i + 1) >= k) {
ret.push_back(nums[q.front()]);
}
}
return ret;
}
int main() {
vector<int> nums;
int k;
vector<int> ret;
nums = {1, 3, -1, -3, 5, 3, 6, 7};
k = 3;
ret = maxSlidingWindow(nums, k);
printVec(ret);
return 0;
}