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;
}
};
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;
}
};
相关文章推荐
- 分数相加减的代码(c++)
- Objective-C 基础一:面向对象过程基础知识
- 微信本地调试、ngrok、tunnel
- 站长工具权重评定标准
- Tcl学习之--流程控制|过程
- 2015年北京大学软件工程学科优秀大学生夏令营上机考试---A:整数序列的元素最大跨度值
- Android之——屏幕适配px转dip
- 南邮 OJ 2096 1_A+B(I)
- 剑指offer刷题之java实现的二维数组中的查找
- spring4集成quartz2.2.1(maven)
- POJ 1861 Network (MST)
- office2013 激活码 亲测有效
- HDU 1312 BFS裸题
- 欢迎使用CSDN-markdown编辑器
- 18个在Linux下监控网络流量的命令(工具)
- 欢迎使用CSDN-markdown编辑器
- 南邮 OJ 2084 送花
- sublime text3使用笔记
- SpringMVC 的<mvc:resources>使用映射路径展示文件服务器上的图片
- Web开发基础知识的整理(一、Http协议的简单介绍)