给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。

以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。

图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。
示例:
输入: [2,1,5,6,2,3]
输出: 10
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/largest-rectangle-in-histogram
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解:
这题我最开始写的和85一种解法,结果最后一个测试用例超时。。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
if(heights.size()==0)
return 0;
int max_area=0;
for(int i=0;i<heights.size();i++)
{
int min_height=INT_MAX;
for(int ass_weight=1;ass_weight<=i+1;ass_weight++)
{
min_height=min(min_height,heights[i-ass_weight+1]);
max_area=max(max_area,min_height*ass_weight);
}
}
return max_area;
}
};
来源:https://www.cnblogs.com/wangshaowei/p/12285236.html