您的位置:首页 > 其它

leetcode 84: Largest Rectangle in Histogram

2015-08-08 11:16 363 查看
Use stack

class Solution {
public:
int largestRectangleArea(vector<int>& height) {
stack<int> s;
int res=0;
height.push_back(0);
for(int i=0;i<height.size();i++)
{
if(s.empty()||height[i]>=height[s.top()])
s.push(i);
else
{
while(!s.empty()&&height[s.top()]>height[i])
{
int h=height[s.top()];
s.pop();
int w=s.empty()?i:i-s.top()-1;
int area=h*w;
res=area>res?area:res;
}
s.push(i);
}
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: