-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmaximum_of_minimum_for_every_window_size.cpp
70 lines (57 loc) · 1.39 KB
/
maximum_of_minimum_for_every_window_size.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
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
// Complete the riddle function below.
vector<long> riddle(vector<long> arr) {
// complete this function
int sz = arr.size();
vector<long> left(sz+1), right(sz+1);
stack<long> s;
vector<long> result(sz+1);
for (int i=0; i<=sz; i++)
result[i] = 0;
for(int i=0; i<sz; i++){
left[i] = -1;
right[i] = sz;
}
for(int i=0; i<sz; i++){
while(!s.empty() && arr[s.top()] >= arr[i])
s.pop();
if(!s.empty())
left[i] = s.top();
s.push(i);
}
while(!s.empty())
s.pop();
for(int i=sz-1; i>=0; i--){
while(!s.empty() && arr[s.top()] >= arr[i])
s.pop();
if(!s.empty())
right[i] = s.top();
s.push(i);
}
for(int i=0; i<sz; i++){
long len = right[i] - left[i] - 1;
result[len] = max(result[len], arr[i]);
}
for(int i=sz-1; i>=1; i--)
result[i] = max(result[i], result[i+1]);
result.erase(result.begin());
return result;
}
int main(){
int T;
cin>>T;
while(T--){
int n;
cin>>n;
vector<long> ip(n);
for(int i=0; i<n; i++){
cin>>ip[i];
}
vector<long> res = riddle(ip);
for(const long &v: res){
cout<<v<<" ";
}
cout<<"\n";
}
}