当前位置:网站首页>Sword finger offer II 039 Histogram maximum rectangular area monotonic stack
Sword finger offer II 039 Histogram maximum rectangular area monotonic stack
2022-06-27 13:54:00 【Python ml】
The finger of the sword Offer II 039. Maximum rectangular area of histogram
The width of the matrix must be , Start from both sides of the column at the top of the stack until you meet the width of the column that is higher than the column .
Because the column height stored in the monotonic stack is increasing , So the column in front of the column on the top of the stack must be shorter than the column on the top of the stack , Similarly, the currently scanned column is also shorter than the column at the top of the stack , So the width of the highest matrix with the top column as the top is determined , Then the area is determined .
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
heights.append(0) # The height of the last column is 0, All the remaining elements of the list will pop Calculate the maximum rectangular area when it is height
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 # When there is only one column element left , Left boundary index It can be regarded as -1
maxArea=max(maxArea,area_height*area_width)
h.append(i) # here heights[i]>heights[h[-1]]
return maxArea
边栏推荐
- Shell concise tutorial
- Interviewer: do you understand redis' shared object pool?
- 剑指 Offer II 039. 直方图最大矩形面积 单调栈
- 为什么 Oracle 云客户必须在Oracle Cloud 季度更新发布后自行测试?
- 重读经典:《The Craft of Research(1)》
- 海外仓知识科普
- Resolve activity startup - lifecycle Perspective
- 实现WordPress上传图片自动重命名的方法
- [weekly replay] the 81st biweekly match of leetcode
- 美国芯片再遭重击,继Intel后又一家芯片企业将被中国芯片超越
猜你喜欢

After 2 years of outsourcing, I finally landed! Record my ByteDance 3 rounds of interviews, hope to help you!

ENSP cloud configuration

外部存储器

初识云原生安全:云时代的最佳保障

全球芯片市场或陷入停滞,中国芯片逆势扩张加速提升自给率

基于 xml 配置文件的入门级 SSM 框架整合

OpenSSF安全计划:SBOM将驱动软件供应链安全

External memory
![[business security-04] universal user name and universal password experiment](/img/09/73d8356d00cefb6d1af086669f69ff.png)
[business security-04] universal user name and universal password experiment

How to solve the problem of missing language bar in win10 system
随机推荐
JVM parameter setting and analysis
AXI總線
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
[business security-02] business data security test and example of commodity order quantity tampering
做一篇人人能搞懂的ThreadLocal(源码)
芯片供给过剩之际,进口最多的中国继续减少进口,美国芯片慌了
Kyndryl与Oracle和Veritas达成合作
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
Why must Oracle cloud customers self test after the release of Oracle cloud quarterly update?
Half find (half find)
Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training
enable_ if
enable_if
【业务安全-04】万能用户名及万能密码实验
PCL库——报错解决:安装时遇到的cmake与anaconda的冲突问题
Pytorch learning 1 (learning documents on the official website)
Summary and Thinking on interface test automation
Does Xinhua San still have to rely on ICT to realize its 100 billion enterprise dream?
crane:字典项与关联数据处理的新思路
力扣 第 81 场双周赛