(数组)Largest Rectangle in Histogram(栈解问题)
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
.
首先想到一种最直观的方法:把平面看做大矩阵,条形看做1,非条形看做0,寻找最大全1矩阵。
思路上是正确的,可以用DP来做,不过会超时。
网上看到一种借助栈的做法,代码很漂亮,但是解释都非常模糊,我看懂之后,决定仔细描述思路如下:
1、如果已知height数组是升序的,应该怎么做?
比如1,2,5,7,8
那么就是(1*5) vs. (2*4) vs. (5*3) vs. (7*2) vs. (8*1)
也就是max(height[i]*(size-i))
2、使用栈的目的就是构造这样的升序序列,按照以上方法求解。
但是height本身不一定是升序的,应该怎样构建栈?
比如2,1,5,6,2,3
(1)2进栈。s={2}, result = 0
(2)1比2小,不满足升序条件,因此将2弹出,并记录当前结果为2*1=2。
将2替换为1重新进栈。s={1,1}, result = 2
(3)5比1大,满足升序条件,进栈。s={1,1,5},result = 2
(4)6比5大,满足升序条件,进栈。s={1,1,5,6},result = 2
(5)2比6小,不满足升序条件,因此将6弹出,并记录当前结果为6*1=6。s={1,1,5},result = 6
2比5小,不满足升序条件,因此将5弹出,并记录当前结果为5*2=10(因为已经弹出的5,6是升序的)。s={1,1},result = 10
2比1大,将弹出的5,6替换为2重新进栈。s={1,1,2,2,2},result = 10
(6)3比2大,满足升序条件,进栈。s={1,1,2,2,2,3},result = 10
栈构建完成,满足升序条件,因此按照升序处理办法得到上述的max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}=8<10
综上所述,result=10

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int ret = 0;
stack<int> stk;
for(int i = 0; i < height.size(); i ++)
{
if(stk.empty() || stk.top() <= height[i])
stk.push(height[i]);
else
{
int count = 0;
while(!stk.empty() && stk.top() > height[i])
{
count ++;
ret = max(ret, stk.top()*count);
stk.pop();
}
while(count --)
stk.push(height[i]);
stk.push(height[i]);
}
}
int count = 1;
while(!stk.empty())
{
ret = max(ret, stk.top()*count);
stk.pop();
count ++;
}
return ret;
}
};

(数组)Largest Rectangle in Histogram(栈解问题)的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
- leetcode之Largest Rectangle in Histogram
问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- LeetCode解题报告—— Minimum Window Substring && Largest Rectangle in Histogram
1. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will ...
- [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 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
题目描述 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的 ...
- Largest Rectangle in Histogram及二维解法
昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- LeetCode 251. Flatten 2D Vector
原题链接在这里:https://leetcode.com/problems/flatten-2d-vector/ 题目: Implement an iterator to flatten a 2d v ...
- CH5E02 [IOI1999]花店橱窗[暴力dp]
众所周知,这个人太菜了,所以她又来切水题了. 显然设计状态表示第$i$朵花放第$j$瓶中的最大价值.然后瞎转移一波是n三方的,加个前缀max变成n方就水过去了. 当然这题可以搜索剪枝的. 虐lyd书上 ...
- AMD 规范
AMD(异步模块定义)是为浏览器环境设计的,因为 CommonJS 模块系统是同步加载的,当前浏览器环境还没有准备好同步加载模块的条件. AMD 定义了一套 JavaScript 模块依赖异步加载标准 ...
- 宝塔中的 base_opendir chattr
宝塔中的 base_opendir chattr base_opendir 目的是限制一些函数将手乱伸. 而宝塔中的自动配置的是 .user.ini,这个是文件是 +i ............ 这个 ...
- 用PowerShell在China Azure创建ARM虚拟机
Azure目前有两种工作模式:ASM和ARM. 在国内的Azure,我们都是使用ASM的模式.但这种模式有很多限制,比如每个VM必须有一个公网地址,部署不能批量部署等等.ARM对Azure的整体架构做 ...
- python3入门
简介 计算机基本概念与程序设计语言分类 python风格指南 基础语法 python3 基础语法:基本输入输出 python3 基础语法:标识符和保留字 python3 基础语法:注释 python3 ...
- ArcGis教程
91卫图助手帮助中心-如何导出ArcGIS Server瓦片格式并进行发布 http://help.91weitu.com/rhdcarcgis%20serverwpgsbjxfb.html ArcG ...
- POJ1020(小正方形铺大正方形)
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16579 Accepted: 5403 ...
- 网页效果分析 VCD分解
VCD分解分为三部分: 1. view 视觉 HTML + CSS 基本界面模板 2. controller 控制 javascript 内 ...
- L2-004. 这是二叉搜索树吗?(前序转后序递归)
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...