首先上题目

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.

For example,
Given height = [2,1,5,6,2,3],
return 10.

题目的大意即给出一个柱状图的歌数据,求柱状图内所包含的最大矩形。

这道题目想了较长时间,助教说用栈的思想一开始也不是很明白(看来真的把数据结构灵活用到实际问题还是很难啊)。后来想用栈的话,目标肯定是O(n),基础操作不外乎前后项比较,根据比较结果进行不同操作,然后有了以下思路。

思路一

初始图表数据项是无序的,用栈进行操作剪掉非最优解后,我们要实现一个逐项递增的效果,如:1 1 1 2 2 4 4……这样的图表无疑是易于计算的。之所以用栈是因为,可以比较得出一个数据n的前后连续数据中,有几个不小于n,压栈完成了前计数,退栈完成了后计数。

LeetCode之Largest Rectangle in Histogram浅析的更多相关文章

  1. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  2. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  3. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

  4. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

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

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

  6. [LeetCode OJ] Largest Rectangle in Histogram

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

  7. [LeetCode#84]Largest Rectangle in Histogram

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

  8. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

  9. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

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

随机推荐

  1. javascript属性标签

  2. JSON http://www.cnblogs.com/haippy/archive/2012/05/20/2509329.html

    js: JSON.stringify(idinfo)//将对象转化为 JSON串 //查询后为将json串赋值给表单 function _form1_load() {            AOS.a ...

  3. HttpContextBase转换成HttpContext对象

    有以下方法: 主要是方式就是通过context获取HttpApplication,然后通过Application获取相应的HttpContext ①HttpContext context=HttpCo ...

  4. Norflash控制器的Verilog建模之一

    摘要:今天驱动一款SPANSION公司生产的norflash——S29AL032D70,没有别的参考资料,大致了解一下norflash的内部cmos电路架构以及其用途之后,直接看手册吧. 如何看手册: ...

  5. URL Regex expression

    转载: http://blog.csdn.net/weasleyqi/article/details/7912647 首先,正则表达式: String check = @"((http|ft ...

  6. javascript的地基

    有了良好的基础,才能在其上创造有价值的东西. 回顾一下以往自己javascript应用的开发经历,似乎很少去思考和总结js的运行机制.现在我就来整理整理 1. 以<编译原理>的一段话开头: ...

  7. oracle 查询执行过的SQL语句

    SELECT * FROM v$sqlarea t WHERE t.FIRST_LOAD_TIME between '2016-12-23/16:03:00' and '2016-12-23/16:0 ...

  8. Jmeter模拟不同带宽

    Jmeter自带模拟带宽设置,当然前提肯定是你当前的带宽>=你要模拟的带宽,好比你装了个4m的宽带,要模拟100m的带宽,那是做梦 做起来也不难,打开user.properties文件,增加如下 ...

  9. Linux手动释放内存

    手动释放内存 1.sync将内存中的缓存写入磁盘 2.  to free pagecache, use echo 1 > /proc/sys/vm/drop_caches;       to f ...

  10. oracle数据库中的基本语句

    下面的都是最基本的oracle数据库的数据查询语句,这是我在网上整理的一份文档,方便以后自己的查看,当然,能把这些记下来就是最好的. 说明:查询表中的数据 1. select * from emp; ...