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.
Example:
Input: [2,1,5,6,2,3]
Output: 10
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
int sum = 0;
for (int i = 0; i < heights.size(); ++i) {
if (i + 1 < heights.size() && heights[i] <= heights[i + 1]) {
continue;
}
int value = heights[i];
for (int j = i; j >= 0; --j) {
value = min(value, heights[j]);
int area = value * (i - j + 1);
sum = max(sum, area);
}
}
return sum;
/*
if(heights.size()==0) return 0;
else if(heights.size()==1) return heights[0];
int sum=0;
int num=0;
int maxval=0;
for(int i=0;i<heights.size();i++)
maxval=max(heights[i],maxval);
for(int i=0;i<heights.size();i++)
maxval=max(heights[i],maxval);
for(int i=1;i<=maxval;i++)
{
for(int j=0;j<heights.size();j++)
if((heights[j]=heights[j]-1)>=0) num++;
else {sum=max(num*i,sum);
num=0;}
sum=max(num*i,sum);
num=0;
}
return sum;
// 第一想法,可惜代码超时了~~~~
*/
}
};
标签:area,int,max,sum,heights,num,Rectangle,Histogram,84 From: https://blog.51cto.com/u_12504263/5718728