当前位置:网站首页>Sword finger offer II 040 Largest rectangle in matrix
Sword finger offer II 040 Largest rectangle in matrix
2022-06-29 04:12:00 【Python ml】
The finger of the sword Offer II 040. The largest rectangle in the matrix
class Solution:
def largestRectangleArea(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
def maximalRectangle(self, matrix: List[str]) -> int:
if not matrix:
return 0
row,col=len(matrix),len(matrix[0])
height,ans=[0]*col,0
for i in range(row):
for j in range (col):
if matrix[i][j]=='0':
height[j]=0
elif i>0 and matrix[i-1][j]=='0': # The rest and matrix[i][j]=1 and matrix[i-1][j]=0
height[j]=int(matrix[i][j])
else: # first line And matrix[i][j]=1 Or The rest And matrix[i][j]=1 And matrix[i-1][j]=1
height[j]+=1
ans=max(ans,Solution.largestRectangleArea(height))
return ans
边栏推荐
- pytorch 读写文件
- Anaconda's own Spyder editor starts with an error
- Logstash starts too slowly or even gets stuck
- HCIE-Security Day41:理论学习:信息收集与网络探测
- Analysis on the types of source code anti leakage technology
- C language -- branch structure
- 中小型企业网络的组建
- Analysis of moudo Network Library
- Yangzhou needs one English IT Helpdesk Engineer -20220216
- 1015 德才论
猜你喜欢

【滤波器设计】根据设计指标使用matlab定制滤波器

基于可变参模板实现的线程池

Here comes Wi Fi 7. How strong is it?

iNFTnews | 元宇宙技术将带来全新的购物体验

moudo网络库剖析

MySQL复习资料(附加)case when

Anaconda自带的Spyder编辑器启动报错问题

Zhai Jia: from technical engineer to open source entrepreneur of "net red"

Basic use of JSX

What is the dry goods microservice architecture? What are the advantages and disadvantages?
随机推荐
【C语言】详解线程回收函数 pthread_join
Webassembly learning - dynamic linking
我的创作纪念日
云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线
sql两列变为多行过滤显示
Why is the test post a giant pit? The 8-year-old tester told you not to be fooled
SEAttention 通道注意力机制
快速开发项目-VScode插件
[C language] address of stack memory associated with local variable 'num' returned
ECS 四 Sync Point、Write Group、Version Number
百度智能云服务网格产品CSM发布 | 火热公测中
SQL database stored procedure writing method
1015 德才论
moudo网络库剖析
Does cdc2.2.1 not support postgresql14.1? Based on the pgbouncer connection mode, with 5433
SEAttention 通道注意力機制
[new function] ambire wallet integrates Metis network
干货丨微服务架构是什么?有哪些优点和不足?
Implementation of b+ tree index based on xlsx
赚钱的5个层次,你在哪一层?