[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 ...
随机推荐
- asp.net 操作INI文件的读写,读写操作本地ini配置文件
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- 线程池ThreadPoolExecutor使用简介
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- windows下安装wamp和wordpress
安装wamp WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过 ...
- jQuery 效果方法
jQuery 效果方法 下面的表格列出了所有用于创建动画效果的 jQuery 方法. 方法 描述 animate() 对被选元素应用"自定义"的动画 clearQueue() 对被 ...
- session cookie 相结合实现
数据库配置文件 config.php <?php// config.php 数据库连接文件define('DB_HOST', 'localhost');define('DB_USER', 'ro ...
- javascript 一串DIV跟随鼠标移动
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- fedora23开发环境搭建手册
chrome安装 [安装chrome教程] nodejs环境搭建 dnf install nodejs dnf install npm sublime text 编辑器安装配置 [fedora安装su ...
- ASP.NET MVC轻教程 Step By Step 7——改进Write动作方法
在上一节我们使用强类型视图改进Write视图获得更好的智能感知和代码重构,现在可以进一步的改进动作方法. Step 1. 数据模型绑定 在Save方法中我们使用Request来获取表单传送的值,其实可 ...
- 业界良心:Square开源Viewfinder,25万行代码全公布!
http://www.csdn.net/article/2014-05-06/2819633-square-open-sources-viewfinder https://github.com/vie ...
- BZOJ 4008 亚瑟王
Description 小K不慎被LL邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂亮.众所周知,亚瑟王是一个看脸的游 ...