[LeetCode OJ] Largest Rectangle in Histogram
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.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example,
Given height = [2,1,5,6,2,3],
return 10.
方法一:两层循环遍历,复杂度O(n2)
class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int maxArea=;
for(unsigned i=; i<height.size(); i++)
{
int min = height[i];
for(unsigned j=i; j<height.size(); j++)
{
if(height[j]<min)
min = height[j];
int area = min*(j-i+);
if(area>maxArea)
maxArea = area;
}
}
return maxArea;
}
};
方法二:用堆栈保存重要位置,复杂度O(n)
class Solution {
public:
int largestRectangleArea(vector<int> &height) { //用堆栈来实现
stack<unsigned> st;
unsigned maxArea = ;
for(unsigned i=; i<height.size(); i++)
{
if(st.empty())
st.push(i);
else
{
while(!st.empty())
{
if(height[i]>=height[st.top()])
{
st.push(i);
break;
}
else
{
unsigned idx=st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = i - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
}
if(st.empty())
st.push(i);
}
}
unsigned rightidx = height.size();
while(!st.empty())
{
unsigned idx = st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = rightidx - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
return maxArea;
}
};
[LeetCode OJ] Largest Rectangle in Histogram的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- Java for LeetCode 084 Largest Rectangle in Histogram【HARD】
For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...
- 关于LeetCode的Largest Rectangle in Histogram的低级解法
在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode之Largest Rectangle in Histogram浅析
首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- SublimeText3使用技巧总结
Ctrl + R : 查找函数和变量 Ctrl + P: 查找当前工程下面的文件 Ctrl + ":" : 查找变量 Ctrl + "g" : 输入行数跳转到指 ...
- android camera(一):camera模组CMM介绍
一.摄像头模组(CCM)介绍: 1.camera特写 摄像头模组,全称CameraCompact Module,以下简写为CCM,是影像捕捉至关重要的电子器件.先来张特写,各种样子的都有,不过我前一段 ...
- 兼容的placeholder属性
作为一个.net后台开发的程序猿,博客里既然大多都是前端相关的博文.是不是该考虑换方向了,转前端开发得了 ... 小小吐槽一下,近期受该不该跳槽所困惑,我有选择困难症! 继续前端,这次说一下输入框 p ...
- Eclipse编译ijkplayer
参考链接:http://blog.csdn.net/fatiao101/article/details/49586379
- Yii中Ajax的使用,如收藏功能
view中 <?php $cs=Yii::app()->clientScript; $cs->registerScriptFile('http://ajax.googleapis.c ...
- Yii - 验证和授权(Authentication and Authorization)
1. 定义身份类 (Defining Identity Class) 为了验证一个用户,我们定义一个有验证逻辑的身份类.这个身份类实现[IUserIdentity] 接口.不同的类可能实现不同的验证 ...
- 问题-安装XP时,提示不识别SATA硬盘
问题现象:笔记本装XP时,系统提示硬盘不能被识别? 问题描述:我原来是vista版本的,硬盘是SATA接口,但我觉得用vista不习惯,所以想装XP,可后来发现机器无法识别硬盘,该怎么解决? 问题原因 ...
- MINA学习之体系介绍
基于MINA应用程序结构图: 我们可以看出,MINA是应用程序(客户端或服务端)和底层基于TCP,UDP等通讯协议的网络层之间的粘合剂.而且各个模块之间是相互独立的,你只需要在MINA体 系基础上设计 ...
- 12种超酷HTML5 SVG和CSS3浮动标签效果
这是一组效果很炫酷的SVG和CSS3表单浮动标签特效.这组浮动标签特效共12种效果,这些浮动标签效果部分在元素的伪元素上使用CSS transitions和CSS animations完毕,一部分则使 ...
- Ubuntu vim+ ctags(包含系统函数) + taglist 配置 分类: vim ubuntu 2015-06-09 18:19 195人阅读 评论(0) 收藏
阅读大型代码,我们经常需要打开很多的代码文件,搜索各种定义.windows下用惯了ide的朋友,转战Linux的时候可能会觉得很难受,找不到合适的阅读工具.其实万能的vim就可以实现.下面介绍一下vi ...