-
Notifications
You must be signed in to change notification settings - Fork 64
/
sol.cpp
51 lines (38 loc) · 812 Bytes
/
sol.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
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> maxSlidingWindow(vector<int> &nums, int k)
{
int n = nums.size();
if (n == 0 || k <= 0)
return {};
vector<int> ans;
deque<int> dq;
for (int i = 0; i < n; i++)
{
if (!dq.empty() && dq.front() == i - k)
{
dq.pop_front();
}
while (!dq.empty() && nums[dq.back()] < nums[i])
{
dq.pop_back();
}
dq.push_back(i);
if (i >= k - 1)
{
ans.push_back(nums[dq.front()]);
}
}
return ans;
}
};
int main()
{
// call the fn here
return 0;
}