84.柱状图中最大的矩形
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
stack<int> s;
int result = heights[0];
heights.insert(heights.begin(),0);
heights.push_back(0);
s.push(0);
for(int i = 1;i<heights.size();i++)
{
while(!s.empty()&&heights[i]<heights[s.top()])
{
int m = s.top();
s.pop();
if(!s.empty())
result = max(result,heights[m]*(i-s.top()-1));
}
s.push(i);
}
return result;
}
};