-
Notifications
You must be signed in to change notification settings - Fork 12
/
LC_102_SlidingWindowMaximum.cpp
32 lines (31 loc) · 1.43 KB
/
LC_102_SlidingWindowMaximum.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
#include <bits/stdc++.h>
using namespace std;
// tc : O(n) sc : O(n)
vector<int> maxSlidingWindow(vector<int> &nums, int k)
{
vector<int> ans;
deque<int> q; // store the index of the maximum elements in the window
// process first k elements - 1st window
for (int i = 0; i < k; i++)
{
// remove the elements which are smaller than the current element because they can't be the maximum element in the window
while (!q.empty() && nums[i] >= nums[q.back()])
q.pop_back();
q.push_back(i); // add the current element as it can be the maximum element in the window and the next window
}
// process remaining windows -> remove the element which is out of the window and add the new the element
for (int i = k; i < nums.size(); i++)
{
ans.push_back(nums[q.front()]); // add the maximum element of the previous window in the answer array
// remove the front element if it is out of the window
if (i - q.front() >= k)
q.pop_front();
// remove the elements which are smaller than the current element because they can't be the maximum element in the window
while (!q.empty() && nums[i] >= nums[q.back()])
q.pop_back();
q.push_back(i); // add the current element as it can be the maximum element in the window and the next window
}
// process the last window
ans.push_back(nums[q.front()]);
return ans;
}