-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path239.sliding-window-maximum.cpp
65 lines (63 loc) · 1.31 KB
/
239.sliding-window-maximum.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
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
/*
* @lc app=leetcode id=239 lang=cpp
*
* [239] Sliding Window Maximum
*/
// @lc code=start
class myqueue {
public:
deque<int>data;
int push(int num) {
if (data.empty()) {
data.push_back(num);
return num;
}
while (!data.empty() && num > data.back()) {
data.pop_back();
}
data.push_back(num);
return num;
}
int pop(int num) {
if (data.front() == num) {
data.pop_front();
}
return num;
}
int max() {
return data.front();
}
};
class Solution {
public:
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
myqueue mq;
vector<int> out;
int front = 0;
for (int i = 0; i < k; i++) {
mq.push(nums[i]);
}
out.push_back(mq.max());
for (int i = 0; i + k < nums.size(); i++) {
mq.pop(nums[i]);
mq.push(nums[i + k]);
out.push_back(mq.max());
}
return out;
}
};
// @lc code=end
int main() {
Solution S1;
vector<int>nums = { 1,3,1,2,0,5 };
int k = 3;
auto tmp = S1.maxSlidingWindow(nums, k);
for (auto i : tmp) {
cout << i << endl;
}
return 0;
}