当前位置:网站首页>剑指 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
边栏推荐
- Realization of hospital medical record management system based on JSP
- 招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
- 【业务安全-01】业务安全概述及测试流程
- How to set postman to Chinese? (Chinese)
- Journal quotidien des questions (6)
- CMOS level circuit analysis
- Explore tidb lightning source code to solve the found bugs
- [XMAN2018排位赛]通行证
- Daily question brushing record (6)
- 力扣 第 81 场双周赛
猜你喜欢

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

NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

Daily question brushing record (6)

Cesium realizes satellite orbit detour

外部存储器

Learning records of numpy Library

美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越

高效率取幂运算

What else can PLM do?

爱可可AI前沿推介(6.27)
随机推荐
EventLoop learning
How ASP connects Excel
Explore tidb lightning source code to solve the found bugs
【业务安全03】密码找回业务安全以及接口参数账号修改实例(基于metinfov4.0平台)
After the deployment is created, the pod problem handling cannot be created
面试官:Redis的共享对象池了解吗?
Interviewer: do you understand redis' shared object pool?
AXI总线
Debug tool
AXI總線
内网学习笔记(8)
一道shell脚本的统计题
解析Activity启动-生命周期角度
[WUSTCTF2020]girlfriend
Awk concise tutorial
每日3题(2):检查二进制字符串字段
After 2 years of outsourcing, I finally landed! Record my ByteDance 3 rounds of interviews, hope to help you!
Crane: a new way of dealing with dictionary items and associated data
Privacy computing fat offline prediction
招标公告:暨南大学附属第一医院Oracle数据库维保服务采购