【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/largest-rectangle-in-histogram/description/
题目描述
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.
Example:
Input: [2,1,5,6,2,3]
Output: 10
题目大意
计算一堆矩形能够成的面积最大的一块是多少。
解题方法
单调栈
这个题是单调栈的运用,使用一个单调递增栈来维护已经出现了的矩形高度。
- 如果后面新来的元素高度比栈顶元素高,那么需要入栈,因为面积最大的元素会出现在后面。
- 如果后面新来的元素高度比栈顶元素小,那么需要弹出栈里的元素,并且,每次弹出的时候都要对计算目前的宽度,相乘得到面积。
栈里保存索引的方式是需要掌握的,保存索引的方式在最小值栈结构中也有运用。
每次求栈内矩形的宽度的时候,其实是求其位置到最右边的距离。注意即将入栈的元素索引 i
是一直不变的,另外栈里的每个元素的索引可以认为是矩形的右边界。
Leetcode #84 Largest Rectangle in Histogram图文并茂,讲得很清楚。
最坏时间复杂度是O(N^2),最优时间复杂度是O(N),空间复杂度是O(N)。
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
stack = list()
res = 0
heights.append(0)
N = len(heights)
for i in range(N):
if not stack or heights[i] > heights[stack[-1]]:
stack.append(i)
else:
while stack and heights[i] <= heights[stack[-1]]:
h = heights[stack[-1]]
stack.pop()
w = i if not stack else i - stack[-1] - 1
res = max(res, h * w)
stack.append(i)
return res
参考资料:
https://www.cnblogs.com/boring09/p/4231906.html
日期
2018 年 10 月 9 日 —— 今天降温7度,注意保暖
【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)的更多相关文章
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [LeetCode#84]Largest Rectangle in Histogram
Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...
- LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形
原题 Given n non-negative integers representing the histogram's bar height where the width of each bar ...
- [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 084 Largest Rectangle in Histogram 柱状图中最大的矩形
给出 n 个非负整数来表示柱状图的各个柱子的高度,每个柱子紧挨彼此,且宽度为 1 .您的函数要能够求出该柱状图中,能勾勒出来的最大矩形的面积. 详见:https://leetcode.com/prob ...
- 84. Largest Rectangle in Histogram
https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...
随机推荐
- R语言与医学统计图形-【25】ggplot图形分面
ggplot2绘图系统--图形分面 ggplot2的分面faceting,主要有三个函数: facet_grid facet_wrap facet_null (不分面) 1. facet_grid函数 ...
- nohup使用
nohup:不挂断运行 在忽略挂起信号的情况下运行给定的命令,以便在注销后命令可以在后台继续运行. 可以这么理解:不挂断的运行,注意并没有后台运行的功能,就是指,用nohup 运行命令可以是命令永远运 ...
- Genscan指南
Genscan指南 GenScan是一个gene识别软件,主要是通过已知生物的基因结构特征来识别新的基因(parse).所利用的基因特征请参看readme文件. 特点: 只考虑编码蛋白的基因. 模型考 ...
- 搭建FastDFS集群
先插一张图(学习的时候找的)http://blog.csdn.net/u012453843/article/details/68957209?> 软件下载地址:主要是fastdfs.libfas ...
- C 语言中求中间数时候防止溢出方法
当使用二分法时候,注意 mid = left + (right - left) / 2; 这句代码,可以防止溢出!!,千万不能写成 mid = (left + right) / 2 这样写的话,当数字 ...
- 零基础学习java------day19-------定时器,线程面试题,Udp,Tcp
0. 定时器 0.1 概述: 定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行,在jaa中,可以通过Timew和TimerTask类来实现定义调度的功能 0.2 Tim ...
- Python实战之MySQL数据库操作
1. 要想使Python可以操作MySQL数据库,首先需要安装MySQL-python包,在CentOS上可以使用一下命令来安装 $ sudo yum install MySQL-python 2. ...
- spring-dm 一个简单的实例
spring-dm2.0 运行环境,支持JSP页面 运行spring web 项目需要引用包
- Vue重要知识
Event Bus 总线 Vue中的EventBus是一种发布订阅模式的实践,适用于跨组件简单通信. Vuex也可以用来组件中进行通信,更适用于多组件高频率通信. 使用方式: 1.把Bus注入到Vue ...
- 关于Mysql java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)的问题
问题所在: 1.连接数据库一个是密码是否正确, 2.driver是否对, 3.有么有jar包冲突,