-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongRec.cpp
More file actions
59 lines (55 loc) · 822 Bytes
/
Copy pathlongRec.cpp
File metadata and controls
59 lines (55 loc) · 822 Bytes
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
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int i,j,max=0;
vector<int> h = {2,1,5,6,2,3};
int n=h.size(),top=-1;
int left[n],right[n],stack[n];
for(i=0;i<n;i++)
{
if(i==0){
top++;
left[i]=0;
stack[top]=0;
}
else{
//j=i-1;
while(top!=-1 && h[i]<=h[stack[top]])
top--;
left[i] = top==-1?0:stack[top]+1;
top++;
stack[top]=i;
}
}
//for(j=0;j<n;j++)
//cout<<left[j]<<"\n";
top = -1;
for(i=n-1;i>=0;i--)
{
if(i==n-1)
{
top++;
right[i] = n-1;
stack[top] = i;
}
else
{
while(top!=-1 && h[i]<=h[stack[top]])
top--;
right[i] = top==-1?n-1:stack[top]-1;
top++;
stack[top]=i;
}
}
int temp;
for(int i=0;i<n;i++)
{
temp = h[i]*(right[i]-left[i]+1);
if(max<temp)
max=temp;
}
cout<<max<<"\n";
}