首先上题目

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. 服务器未能识别 HTTP 标头 SOAPAction 的值

    SOAPAction HTTP request header被用来标识SOAP HTTP请求的目的地,其值是个URI地址.SOAP发送并不限制格式.URI特征或其必须可解析,那么在这种情况下,发送一个 ...

  2. .Net 连接字符串的解释

    https://msdn.microsoft.com/zh-cn/library/cc716756.aspx 连接字符串参数 连接字符串的格式是使用分号分隔的键/值参数对列表: keyword1=va ...

  3. 通过Nginx+tomcat+redis实现反向代理 、负载均衡及session同步

    一直对于负载均衡比较陌生,今天尝试着去了解了一下,并做了一个小的实验,对于这个概念有一些认识,在此做一个简单的总结 什么是负载均衡 负载均衡,英文 名称为Load Balance,指由多台服务器以对称 ...

  4. session 丢失和解决方案

    在ASP.NET的开发中,总遇到Session丢失.最常见的情况是当用户登录后将用户信息保存在Session中例如Session["user"]=user;在其后的页面中检查Ses ...

  5. OC编程的一些UI细节

    1/如果你想用一个半透明的View遮住当前窗口,连并NavigationBar也一并遮住的话,那么你需要 将视图添加到navigationController的View上 [self.navigati ...

  6. java学习第16天(泛型 增强for)

    泛型概述 是一种把明确类型的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型.格式: <数据类型> 注意:该数据类型只能是引用类型. 好处:避免了强制类型转换,比如上个实验   S ...

  7. 命令与文件的查询 which whereis locate find

    一:which which查找脚本文件也就是我们的系统命令 用法:which [command] 默认根据我们PATH路径去查找,但是不同的用户肯定设置不同的PATH,所以就像普通用户查找不到一些/s ...

  8. python字符串及其方法详解

    首先来一段字符串的基本操作 str1="my little pony" str2="friendship is magic" str3=str1+", ...

  9. arp命令

    地址解析协议ARP用于将IP地址解析成MAC地址.当把数据包从一个计算机发送到另一个计算机的时候,计算机或路由器使用ARP请求来确定下一跳的MAC地址. MAC地址用于按跳发送数据包,直到达到最终目的 ...

  10. Python-lambda函数,map函数,filter函数

    lambda函数主要理解: lambda 参数:操作(参数). lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值.lambda语句构建的其实是一个函数对象 map函数: ma ...