forked from shivammaniharsahu/coding_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram.cpp
More file actions
68 lines (67 loc) · 1.18 KB
/
histogram.cpp
File metadata and controls
68 lines (67 loc) · 1.18 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int n,res=INT_MIN;
cin>>n;
vector<int> v(n),pre,suf;
stack<pair<int,int>> s,t;
for(int i=0;i<n;i++){
cin>>v[i];
}
for(int i=0;i<n;i++){
if(s.empty()){
pre.push_back(-1);
}
else if(s.top().first<v[i]){
pre.push_back(s.top().second);
}
else if(!s.empty() and s.top().first>=v[i] ){
while(!s.empty() and s.top().first>=v[i]){
s.pop();
}
if(s.empty()){
pre.push_back(-1);
}
else{
pre.push_back(s.top().second);
}
}
s.push({v[i],i});
}
for(int i=n-1;i>=0;i--){
if(t.empty()){
suf.push_back(n+1);
}
else if(t.top().first<v[i]){
suf.push_back(t.top().second);
}
else if(!t.empty() and t.top().first>=v[i] ){
while(!t.empty() and t.top().first>=v[i]){
t.pop();
}
if(t.empty()){
suf.push_back(n+1);
}
else{
suf.push_back(t.top().second);
}
}
t.push({v[i],i});
}
reverse(suf.begin(),suf.end());
int temp;
for(int i=0;i<n;i++){
temp = v[i]*(suf[i]-pre[i]-1);
res=max(res,temp);
}
for(int i=0;i<n;i++){
cout<<pre[i]<<" ";
}
cout<<"\n";
for(int i=0;i<n;i++){
cout<<suf[i]<<" ";
}
cout<<"\n";
cout<<res;
}