-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreatestElementInWindow.cpp
More file actions
45 lines (40 loc) · 1 KB
/
greatestElementInWindow.cpp
File metadata and controls
45 lines (40 loc) · 1 KB
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
#include<bits/stdc++.h>
using namespace std;
// greatest element in window of size k
void naive(vector<int> a, int k){
for(int i = 0; i < a.size()-k+1; i++){
int m = a[i];
for(int j = 0; j < k; j++){
m = max(m, a[i+j]);
}
cout << m << " ";
}
}
void optimal(vector<int> a, int k){
deque<int> d;
for(int i = 0; i < k; i++){
while(!d.empty() && arr[d.back()] <= arr[i]){
d.pop_back();
}
d.push_back(i);
}
for(int i = k; i < n; i++){
cout << a[d.front()] << " ";
// remove elements which are not part of curr window
while(!d.empty() && d.front() <= i-k){
d.pop_front();
}
// remove all small elements
while(!d.empty() && arr[d.back()] <= arr[i]){
d.pop_back();
}
d.push_back(i);
}
cout << a[d.front()] << '\n';
}
int main(){
vector<int> a = {10, 8, 5, 12, 15, 7, 6};
int k = 3;
optimal(a, k);
return 0;
}