LeetCode: 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.
SOLUTION 1:
http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html
使用递增栈来处理。每次比较栈顶与当前元素。如果当前元素小于栈顶元素,则入站,否则合并现有栈,直至栈顶元素小于当前元素。结尾入站元素0,重复合并一次。
1. 当遇到一个违反递增关系的元素时,例如:
2, 1, 5, 6, 2, 3
(1). S: 2
(2). S: 2 新的元素1比2要小,表示Index = 0的这个直方图的右边界到达了,我们可以计算以它高度的最大直方。那么这个宽度如何计算呢?
i = 1, 而2弹出后,栈为空,宽度是从i到最左边(因为这是一个递增栈,如果现在栈为空,表示我们取出的当前直方是最低的直方,它的宽度可以一直延展到最左边。)
假如栈不为空,则宽度是 i - s.peek() - 1 (因为要减去s.peek()这个直方本身,它比s.pop()要低,阻止了s.pop()这个直方向左边延展)
2 弹出后,栈为空。
(3). S: 1, 5, 6(这三个是连续递增,就让它们一直入栈)
(4). S: 1, 5, 6 现在我们遇到2, 6 出栈,它的宽度是6本身(i - 5所在的索引- 1)。5出栈,宽度是i - 1所在的索引 - 1
(5). 因为2比1要高,所以我们停止计算直方,把2继续入栈。然后3入栈。
(6). 这时index = len. 我们直接到第二个分支,把1, 2, 3 这三个直方计算了。
(7). 栈为空,index = len 会入栈,然后index++ 越界,下一次就退出了。这里我们不可以把index < len 放在第一个判断的前面来判断,因为这样的话,当index = len,
会直接再进入第二个分支,引发越界错误。其实我们就是假设在整个直方的最后存在一个height = 0的直方,所以我们要在一直计算到Index = len为止。而且因为它高度为0比谁都要低,所以可以把这个索引直接入栈。
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/array/LargestRectangleArea.java
LeetCode: Largest Rectangle in Histogram 解题报告的更多相关文章
- [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 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 ...
随机推荐
- 【DeepLearning】用于几何匹配的卷积神经网络体系结构
[论文标题]Convolutional neural network architecture for geometric matching (2017CVPR) [论文作者]Ignacio Rocc ...
- apache配置中ProxyPassReverse指令的含义
apache中的mod_proxy模块主要作用就是进行url的转发,即具有代理的功能.应用此功能,可以很方便的实现同tomcat等应用服务器的整合,甚者可以很方便的实现web集群的功能. 例如使用ap ...
- 进阶之路(基础篇) - 003 I/O的模拟的读取
/********************************* 代码功能:读取某引脚的模拟量串口返回数据 使用函数: analogRead(引脚号); //调用10位AD 创作时间:2016*1 ...
- windbg(1)
1.http://www.cnblogs.com/huangyong9527/category/384128.html 2.http://www.cnblogs.com/pugang/category ...
- 【AaronYang风格】第一篇 CodeFirst 初恋
原著:Prorgamming Entity Framework Entitywork Code First 大家好! 我是AaronYang,这本书我也挺喜欢的,看了一半了,今晚也没 ...
- php 将秒数转换为时间(年、天、小时、分、秒)
$t=1637544; $d=Sec2Time($t); $d为 0年18天 22小时52分24秒 //将秒数转换为时间(年.天.小时.分.秒) function Sec2Time($time){ ...
- iphone app的非appstore发布方法及其免越狱安装方法
iphone app的非appstore发布方法及其免越狱安装方法 本文包含两项内容, 1.开发者如何将app导出为可供普通用户在外部安装的ipa文件. 2.用户使用itools来安装ipa格式的 ...
- JAX-RS annotations
@Path("resource_path"):The @Path annotation defines the path to the base URL or resource_p ...
- AndroidUI设计 之 图片浏览器
图片浏览器效果图 : 源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6875083 -- GitHub : https:/ ...
- 转 redis 锁
原文链接: http://www.promptness.cn/article/34 前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的 ...