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 解题报告的更多相关文章

  1. [LeetCode] Largest Rectangle in Histogram 解题思路

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  3. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  4. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  5. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  7. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  8. 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 ...

  9. LeetCode——Largest Rectangle in Histogram

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...

随机推荐

  1. 使用tmpfs的好处

    EBS上超高频率的IO写入 这几天注意到我的EC2机器上有非常大量的IO,导致AWS的总体开销增加了很多.比如9月份的帐单中: I/O requests     120,076,984 IOs     ...

  2. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  3. 学习asp.net的流程

    如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 掌握一门.NET面向对象语言,C#或VB.NET 我强烈反对在没系统学过一门面向对象(OO)语言的前提下去学ASP.NET. ASP.NE ...

  4. Python 爬虫实例(13) 下载 m3u8 格式视频

    Python  requests  下载  m3u8 格式    视频 最近爬取一个视频网站,遇到  m3u8 格式的视频需要下载. 抓包分析,视频文件是多个  ts 文件,什么是 ts文件,请去百度 ...

  5. Android Activity全面解析

    Android Activity全面解析 首先,就从Android四大组件Activity开始. 1.Activity生命周期方法完全解析   activity_lifecycle.png 1).on ...

  6. spring 项目中在类中注入静态字段

    有时spring 项目中需要将配置文件的属性注入到类的静态字段中 例如:文件上传 //文件上传指定上传位置 //resource-dev.properties 有如下参数 #upload UPLOAD ...

  7. php分享二十三:字符编码

    1:ASCII 在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写).以及0.1等数字还有一些常用的 ...

  8. Jackson(使用注解)

    jackson在实际应用中给我们提供了一系列注解,提高了开发的灵活性,下面介绍一下最常用的一些注解 @JsonIgnoreProperties此注解是类注解,作用是json序列化时将Java bean ...

  9. Windows平台JDK安装

    原文链接:http://android.eoe.cn/topic/android_sdk 下载Java的开发包JDK JDK有好几个类型版本,我们只需要选择Java SE类型的版本就行了.进入网页:h ...

  10. [CTCI] 最长合成字符串

    最长合成字符串 题目描述 有一组单词,请编写一个程序,在数组中找出由数组中字符串组成的最长的串A,即A是由其它单词组成的(可重复)最长的单词. 给定一个string数组str,同时给定数组的大小n.请 ...