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. HTTPS 指南

    苹果在 WWDC 2016 上宣布:2016 年底将要求所有 APP 适配苹果的 App Transport Security,简单地说就是除了特殊情况(浏览器.第三方服务.媒体)外,APP 跟服务端 ...

  2. 工程部署从tomcat6.0迁移到jboss6.0错误总结

    2014-11-08 内容存档在evernote,笔记名"工程部署从tomcat6.0迁移到jboss6.0错误总结"

  3. atexit函数和exit函数的理解

    按照ISO C的规定,一个进程可以登记多达32个函数,这些函数将由exit自动调用,通常这32个函数被称为终止处理程序,并调用atexit函数来登记这些函数. 我们通常认为C语言的起始函数是main函 ...

  4. JsonPath小结

    在查看DHC Assertions 模块说明的时候,无意间发现assert模块中JsonBody使用了 JSON Path ,兴趣使然,看了下,发现是类似解析xml用到的 XPath.通过路径来获取j ...

  5. 《JAVA与模式》之备忘录模式

    一.备忘录(Memento)模式结构 备忘录对象是一个用来存储另外一个对象内部状态的快照(snapshot)的对象.备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉住,并外部化,存储起来, ...

  6. Linux C 编程一站式学习

    个人认为这是一个挺不错的从C语言到Linux系统开发的教程,这本是两个网上的文档. 其中 一本是<How To Think Like A Computer Scientist: Learning ...

  7. C++的坑真的多吗?

    先说明一下,我不希望本文变成语言争论贴.希望下面的文章能让我们客观理性地了解C++这个语言.(另,我觉得技术争论不要停留在非黑即白的二元价值观上,这样争论无非就是比谁的嗓门大,比哪一方的观点强,毫无价 ...

  8. 《跟老男孩学Linux运维:Web集群实战》读书笔记

    Linux 介绍 Linux 安装 Linux 调优 Web 基础 Nginx 应用 LNMP 应用 PHP 缓存加速 Nginx 调优 MySQL 应用 NFS 网络文件共享 Nginx 反向代理与 ...

  9. jms异步转同步调用实例

    思路: 当主线程调用异步方法时,将自己挂起,并把引用交给jms的监听: 当监听收到返回的消息时,处理并唤醒主线程继续执行(可以获取和处理返回的消息) Test.java package com.my. ...

  10. 三角函数 与 JavaScript

    三角函数   canvas 和 JavaScript 中所有与角相关的API如Math.sin().Math.cos().Math.tan(),都需要以弧度为单位值.但大部分人还是习惯以角度单位.所以 ...