-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxAreaHistogram.cpp
54 lines (53 loc) · 1.03 KB
/
MaxAreaHistogram.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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
long long int maxhist(vector <int> a)
{
int n=a.size();
stack <int> s;
vector <int> left(n,-1);
vector <int> right(n,n);
for(int i=0;i<n;i++)
{
while(!s.empty() && a[s.top()]>a[i])
{
right[s.top()]=i;
s.pop();
}
s.push(i);
}
while(!s.empty())
s.pop();
for(int i=n-1;i>=0;i--)
{
while(!s.empty() && a[s.top()]>a[i])
{
left[s.top()]=i;
s.pop();
}
s.push(i);
}
long long int area,maxarea=a[1];
for(int k=0;k<n;k++)
{
area=(right[k]-left[k]-1)*a[k]*1LL;
if(area>maxarea)
maxarea=area;
}
return maxarea;
}
int main()
{
vector <int> a;
int n,val;
cin>>n;
cout << "Enter heights :" << endl;
for(int i=0;i<n;i++)
{
cin>>val;
a.push_back(val);
}
cout<<maxhist(a);
return 0;
}