[LeetCode] 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
.
[ 解题思路 ]
问题: 求直方图中面积最大的矩形。
直方图中面积最大的矩形,必然以某一个柱作为高,左侧、右侧最近且矮于该柱的柱为宽边界。
考虑上面长度为 7 的直方图(图片来源), {6, 2, 5, 4, 5, 2, 6}。面积最大矩形的是红框中的矩形,面积为 12 。
方案:
“i最大矩形”,表示为 i 柱为高,左侧、右侧最近且矮于该 i 柱的柱为宽边界的矩形。
"iLeft" , 表示左侧最近且矮于 i 柱的柱
"iRight", 表示右侧最近且矮于 i 柱的柱
第一步,分别求出 n 个 “i最大矩形”,( i : 0->(n-1) )。第二步,找过第一步中最大值,即为原问题的解。
若每次单独求 i 柱的 "iLeft", "iRight",则算法复杂度为 O(n*n)。可以利用栈 s ,巧妙地将时间复杂度降为 O(n)。
- 当栈为空 或 s.peak < h[i] 时,则将 i 压入栈顶。i++。
- 当 h[i] <= s.peak 时,对于 s.peak 柱来说, h[i] 为 "iRight", 栈中 s.peak 的前一个柱为 "iLeft",则弹出 s.peak,并计算 s.peak 的 "i最大矩形"
int largestRectangleArea(vector<int>& height) { int maxArea = ; vector<int> stack; int i = ;
while ( i < height.size() ) {
if (stack.size() == || height[stack.back()] < height[i]) {
stack.push_back(i);
i++;
}else{
int tmpH = height[stack.back()];
stack.pop_back(); int tmpW = stack.empty() ? i : (i - stack.back() - ); int area = tmpH * tmpW;
maxArea = max(area, maxArea);
}
} while ( !stack.empty() ) {
int tmpH = height[stack.back()];
stack.pop_back(); int tmpW = stack.empty() ? (int)height.size() : (int)height.size() - stack.back() - ; int area = tmpH * tmpW;
maxArea = max(area, maxArea);
} return maxArea;
}
参考资料:
Largest Rectangular Area in a Histogram | Set 2, GeeksforGeeks
[LeetCode] Largest Rectangle in Histogram 解题思路的更多相关文章
- LeetCode: Largest Rectangle in Histogram 解题报告
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- [leetcode]Largest Rectangle in Histogram @ Python
原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...
- [LeetCode] 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(直方图最大面积)
http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...
- [LeetCode] 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 TODO O(N)
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- LeetCode——Largest Rectangle in Histogram
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
随机推荐
- 数据库性能高校:CPU使用过高(下)
CPU使用率过高的常见原因 查询优化器会尽量从CPU,IO和内存资源成本最小的角度,找到最高效的数据访问方式.如果没有正确的索引,或者写的语句本身就会忽略索引, 又或者不准确的统计信息等情况下,查询计 ...
- hibernate 使用in方式删除数据
1当删除一个表中数据时,可能会涉及中间表,中间表会有多条数据.这时删除可以采用for循环,逐条删除.但是每次删除都会连接一次数据库 2.可以采用in语句,一次删除即可,参考如下博文 http://ne ...
- why slow thinking wins
今天Hacker News上的一篇文章<为什么想得慢的人能赢>引起了广泛的讨论. 网友Scott Burson在文章后评论说:"之前,我雇佣了一位TopCoder冠军,原本预计他 ...
- SQL,学习基础2
列=字段, 记录=实体 事物日志文件(用来记录数据库的增删情况,扩展名LDF) 数据库文件(但是只有一个是主数据库文件(即用它来启动的),其余为次数据库文件)mdf 数据类型: 整形(整数)——in ...
- 对装饰模式(Decorator)的解读
看过好多对装饰模式的讲解,他们几乎都有一句相同的话:对现有类功能的扩展.不知道大家怎么理解这句话的,之前我把”对功能的扩展“理解成”加功能=加方法“,比如Person类本来有两个功能:Eat 和 Ru ...
- Dreamweaver安装jQuery插件jQuery_API.mxp
要让Dreamweaver支持jQuery自动提示代码功能,方法很简单,下载一个插件—jQuery_API.mxp[点击下载]. 在Dreamweaver里依次选择“命令” -> “扩展管理” ...
- [CSS]position定位
CSS position 属性 通过使用 position 属性,我们可以选择 4 种不同类型的定位,这会影响元素框生成的方式. position 属性值的含义: static 元素框正常生成.块级元 ...
- Activity singleTop启动模式
栈顶单例模式 和standard模式一样, 只有Activity已经存在并且位于栈顶时, 不会重新创建. 其他时候都会创建新的Activity,然后放在栈顶
- highcharts-Highmaps 动态传入城市名称
做前端按地区(地图)分布监控数据展示用了 HIGHMAPS JAVASCRIPT MAPS 控件,很好很强大. 基础实现是这样的:调用插件动态传入需要展示的数据(data),插件会在地图数据(mapd ...
- LightOJ_1248 Dice (III)
题目链接 题意: 给一个质地均匀的n的骰子, 求投掷出所有点数至少一次的期望次数. 思路: 这就是一个经典的邮票收集问题(Coupon Collector Problem). 投掷出第一个未出现的点数 ...