84. 柱状图中最大的矩形
解法1:
class Solution {
public:
int largestRectangleArea(vector<int>& heights)
{
if(heights.size() == 0) return 0;
if(heights.size() == 1) return heights[0];
int ans = 0;
heights.insert(heights.begin(),0);
heights.push_back(0);
stack<int> st;
st.push(0);
for(int i = 1;i < heights.size(); ++ i)
{
while(!st.empty() && heights[i] < heights[st.top()])
{
int height = heights[st.top()];
st.pop();
if(heights[st.top()] < height )
{
ans = max(ans,height*(i - st.top() - 1));
}
}
st.push(i);
}
return ans;
}
};