LeetCode---(84)Largest Rectangle in Histogram
2015-06-23 23:22
459 查看
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram.png)
Above is a histogram where width of each bar is 1, given height =
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram_area.png)
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
class Solution {
public:
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int largestRectangleArea(vector<int>& height) {
height.push_back(0);
stack<int> s;
int result=0;
int temp;
for(int i=0;i<height.size();)
{
if(s.empty()||height[i]>height[s.top()])
{
s.push(i);
i++;
}
else{
temp=s.top();
s.pop();
result=max(result,height[temp]*(s.empty()?i:i-s.top()-1));
}
}
return result;
}
};
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram.png)
Above is a histogram where width of each bar is 1, given height =
[2,1,5,6,2,3].
![](http://www.leetcode.com/wp-content/uploads/2012/04/histogram_area.png)
The largest rectangle is shown in the shaded area, which has area =
10unit.
For example,
Given height =
[2,1,5,6,2,3],
return
10.
class Solution {
public:
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int largestRectangleArea(vector<int>& height) {
height.push_back(0);
stack<int> s;
int result=0;
int temp;
for(int i=0;i<height.size();)
{
if(s.empty()||height[i]>height[s.top()])
{
s.push(i);
i++;
}
else{
temp=s.top();
s.pop();
result=max(result,height[temp]*(s.empty()?i:i-s.top()-1));
}
}
return result;
}
};
相关文章推荐
- Linux设置环境变量命令
- 【读书笔记:C++ primer plus 第六版 中文版】第10章 对象和类
- 用SRS和FFMPEG进行直播流转码
- Log4 配置简介
- android---简单的计时器
- Android 四大组件 简介
- 【练习题】编写一个函数,把一个char组成的字符串循环右移n位
- Android基础教程(一)--Android工程目录结构
- Linux TC(Traffic Control) 简介(一)
- Linux常用命令
- 黑马程序员--汉诺塔问题的递归求解C语言
- 既然阿里不能跟美团“在一起”,至少还有“口碑”一条街
- 【一些事晚报】Uber刷单调查:量级达百万
- MyEclipse无法部署项目
- dell灵越14TD-5628特别版安装OS X Yosemite 10.10.3 (14D131) 懒人版流程备忘
- MyEclipse无法部署项目
- 一起学编程(5--流程)
- MySQL计划任务(事件调度器)(Event Scheduler)[转]
- Java中的Checked Exception——美丽世界中潜藏的恶魔?
- 数据结构和算法经典100题-第19题