当前位置:网站首页>剑指 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
边栏推荐
- Number of printouts (solved by recursive method)
- CMOS level circuit analysis
- Privacy computing fat offline prediction
- 面试官:Redis的共享对象池了解吗?
- Domestic database disorder
- MySQL locking mechanism and four isolation levels
- 解析Activity启动-生命周期角度
- 如何使用200行代码实现Scala的对象转换器
- High efficiency exponentiation
- What if the win system cannot complete the update and is revoking the status change
猜你喜欢

Array related knowledge

How to set the compatibility mode of 360 speed browser

Quick news: Huawei launched the Hongmeng developer competition; Tencent conference released the "Wanshi Ruyi" plan

Embedded development: embedded foundation callback function

EventLoop learning

Summary and Thinking on interface test automation

Openhgnn releases version 0.3

【业务安全-04】万能用户名及万能密码实验

enable_if

How to solve the problem of missing language bar in win10 system
随机推荐
Crane: a new way of dealing with dictionary items and associated data
Pytorch learning 3 (test training model)
A pang's operation record
【问题解决】Tensorflow中run究竟运行了哪些节点?
IJCAI 2022 | 用一行代码大幅提升零样本学习方法效果,南京理工&牛津提出即插即用分类器模块
Postman如何设置成中文?(汉化)
解析Activity启动-生命周期角度
Pre training weekly issue 51: reconstruction pre training, zero sample automatic fine tuning, one click call opt
PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
Completely solve the problem of Chinese garbled code in Web Engineering at one time
Openhgnn releases version 0.3
力扣 第 81 场双周赛
赛迪顾问发布《“十四五” 关键应用领域之数据库市场研究报告》(附下载)
【OS命令注入】常见OS命令执行函数以及OS命令注入利用实例以及靶场实验—基于DVWA靶场
Array related knowledge
基于 xml 配置文件的入门级 SSM 框架整合
jvm 性能调优、监控工具 -- jps、jstack、jmap、jhat、jstat、hprof
外部存储器
Hardware development notes (VII): basic process of hardware development, making a USB to RS232 module (VI): creating 0603 package and associating principle graphic devices
crane:字典项与关联数据处理的新思路