当前位置:网站首页>剑指 Offer II 040. 矩阵中最大的矩形
剑指 Offer II 040. 矩阵中最大的矩形
2022-06-29 04:08:00 【Python ml】
class Solution:
def largestRectangleArea(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
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': #其余行 and matrix[i][j]=1 and matrix[i-1][j]=0
height[j]=int(matrix[i][j])
else: #第一行 且 matrix[i][j]=1 Or 其余行 且 matrix[i][j]=1 且 matrix[i-1][j]=1
height[j]+=1
ans=max(ans,Solution.largestRectangleArea(height))
return ans
边栏推荐
- 基于xlsx的B+树索引实现
- Implementation of b+ tree index based on xlsx
- 赚钱的5个层次,你在哪一层?
- Technology: how to design zkvm circuit
- 【C语言】 详解线程退出函数 pthread_exit
- 数据库和缓存如何保持一致性
- Qtableview gets all currently selected cells
- 项目开发修养
- Draft competition process of Intelligent Vision Group
- On June 27, 2022, I have the right to choose the journey of the summer vacation.
猜你喜欢

【FPGA数学公式】使用FPGA实现常用数学公式
![[new function] ambire wallet integrates Metis network](/img/29/8a8c0cd40c51cef1174ee59706d4c9.png)
[new function] ambire wallet integrates Metis network

Tech Cloud Report: Mixed Office B side: How Safety and Efficiency can be combined?

数据库和缓存如何保持一致性

人大金仓(KingBase)导出表结构

Anaconda's own Spyder editor starts with an error

How to keep database and cache consistent

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

How to merge upstream and downstream SQL data records

你为什么做测试/开发程序员?还能回想出来吗......
随机推荐
Runtimeerror in yolox: dataloader worker (PID (s) 17724, 1364, 18928) exited unexpectedly
数据库和缓存如何保持一致性
Technology: how to design zkvm circuit
2022年 6月27号 《暑假感悟篇一》路程的选择权。
要不是和阿里P7聊过,我也不知道自己是个棒槌
HCIE-Security Day41:理论学习:信息收集与网络探测
Ansible最佳实践之Playbook不同上下文提权Demo
Live broadcast preview | neurips special session I & Young Scientists special session
SEAttention 通道注意力機制
[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber
【C语言】详解线程回收函数 pthread_join
Ling Jing thinks about her own way
Technology cloud report: side B of mixed office: how to have both security and efficiency?
Microsecond TCP timestamp
SqlServer如何查询除去整列字段为null的结果
sink端 一般都是jdbc的insert update delete么?
Emotional changes need to be controlled
大神们 在富函数的open中从mysql连接池里取连接 连接池初始化是20个 如果富函数的并行度是1
Data collection and management [1]
Pytorch read / write file