Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]
.
The largest rectangle is shown in the shaded area, which has area = 10
unit.
For example,
Given height = [2,1,5,6,2,3]
,
return 10
.
class Solution {
public:
int largestRectangleArea(vector<
int> &height)
{
int max=
0;
int minl[height.size()];
int minr[height.size()];
for(
int i=
0;i<height.size();i++)
{
int index=i-
1;
while(index>=
0 && height[index]>=height[i]) index=minl[index]-
1;
minl[i]=index+
1;
}
for(
int i=height.size()-
1;i>=
0;i--)
{
//
right
int index=i+
1;
while(index<height.size() && height[index]>=height[i]) index=minr[index]+
1;
minr[i]=index-
1;
}
for(
int i=
0;i<height.size();i++)
if((minr[i]-minl[i]+
1)*height[i]>max)
max=(minr[i]-minl[i]+
1)*height[i];
return max;
}
};