当前位置:网站首页>剑指 Offer II 039. 直方图最大矩形面积 单调栈
剑指 Offer II 039. 直方图最大矩形面积 单调栈
2022-06-27 13:43:00 【Python ml】
剑指 Offer II 039. 直方图最大矩形面积
该矩阵的宽度一定是,从栈顶柱子的两边出发直到遇到比该柱高矮的柱子所夹成的宽度。
因为单调栈中保存的柱高是递增的,所以栈中位于栈顶柱子前面的柱子一定比栈顶柱子矮,同样当前扫描到的柱子也矮于位于栈顶的柱子,所以顶柱子为顶的最高矩阵的宽度就确定了,那么面积也就确定了。
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
heights.append(0) #最后一个柱形高度为0,列表剩下的元素全都会pop出计算其为高度时的最大矩形面积
lenh=len(heights)
maxArea=0
h=[-1]
for i in range (lenh):
while h[-1]!=-1 and heights[i]<heights[h[-1]]:
area_height=heights[h[-1]]
h.pop()
area_width=i-h[-1]-1 #只剩一个柱形元素时,左边界index可当作-1
maxArea=max(maxArea,area_height*area_width)
h.append(i) #此时heights[i]>heights[h[-1]]
return maxArea
边栏推荐
猜你喜欢

爱可可AI前沿推介(6.27)
![[weekly replay] the 81st biweekly match of leetcode](/img/66/03ee4dbb88b0be7486b71cd4059f44.png)
[weekly replay] the 81st biweekly match of leetcode

AXI总线

【业务安全-02】业务数据安全测试及商品订购数量篡改实例

AGCO AI frontier promotion (6.27)

After the deployment is created, the pod problem handling cannot be created

【OS命令注入】常见OS命令执行函数以及OS命令注入利用实例以及靶场实验—基于DVWA靶场

Crane: a new way of dealing with dictionary items and associated data

一次性彻底解决 Web 工程中文乱码问题

Teach you how to build a permanent personal server!
随机推荐
How to set postman to Chinese? (Chinese)
命令行编辑器 sed 基础用法总结
Crane: a new way of dealing with dictionary items and associated data
POSIX AIO -- Introduction to glibc version asynchronous IO
线程同步之信号量
【业务安全-02】业务数据安全测试及商品订购数量篡改实例
PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
Shell concise tutorial
一次性彻底解决 Web 工程中文乱码问题
Kyndryl与Oracle和Veritas达成合作
How to solve the problem of missing language bar in win10 system
Yyds dry goods inventory solution sword finger offer: cut rope (advanced version)
Read a poem
【OS命令注入】常见OS命令执行函数以及OS命令注入利用实例以及靶场实验—基于DVWA靶场
A pang's operation record
Summary of basic usage of command line editor sed
Firewall foundation Huawei H3C firewall web page login
Can flush open an account for stock trading? Is it safe?
MySQL index and its classification
ensp云朵配置