-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path25_1_sliding_window_maximum_using_deque.cpp
61 lines (48 loc) · 1.42 KB
/
25_1_sliding_window_maximum_using_deque.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
#include<bits/stdc++.h>
#include<deque>
using namespace std;
/*
Slding Window Maximum
[1,3,-1,-3,5,3,6,7]
K=3
[1,3,-1] => 3
[3,-1,-3] => 3
[-1,-3,5] => 5
[-3,5,3] => 5
[5,3,6] => 6
[3,6,7] => 7
Time Complexity is O(n).
Input = 8 3 1 3 -1 -3 5 3 6 7
*/
int main()
{
int n,k;
cin>>n>>k;
vector<int> a(n);
for(auto &i:a){
cin>>i;
}
deque<int> q; // Indices will be stored here, not elements
vector<int> ans;
for(int i=0;i<k;i++){ // For first k elements
while(!q.empty() and a[q.back()] < a[i]){ // If the current element is greater than the last element in dequeue, then remove the last element's index from dequeue and push the current elements's index into the dequeue.
q.pop_back(); // This while loop ensures that the elements stored in dequeue are in Descending order
}
q.push_back(i); // Add the index of newly discovered maximum element
}
ans.push_back(a[q.front()]); // Push the maximum element into the ans vector.
for(int i=k;i<n;i++){
if(q.front()==i-k){ // Elements at index i-k will be removed, since they are not part of current sliding window.
q.pop_front();
}
while(!q.empty() and a[q.back()] < a[i]){ // similar to previous steps
q.pop_back();
}
q.push_back(i);
ans.push_back(a[q.front()]);
}
for(auto i:ans){
cout<<i<<" ";
}
return 0;
}