当前位置:网站首页>剑指 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
边栏推荐
- Data collection and management [4]
- 【Laravel系列8】走出 Laravel 的世界
- 云原生周报 | Grafana 9正式发布;云原生词汇表中文版现已上线
- Data collection and management [13]
- Data collection and management [3]
- Libuv库概述及libevent、libev、libuv对比(转载)
- Nuxt - set SEO related tags, page titles, icons, etc. separately for each page (page configuration head)
- HCIE-Security Day41:理论学习:信息收集与网络探测
- NotImplementedError: Could not run torchvision::nms
- Mécanisme d'attention du canal de fixation
猜你喜欢

Technology: how to design zkvm circuit

If I hadn't talked to Ali P7, I wouldn't know I was a mallet

Wi-Fi 7 来啦,它到底有多强?

干货丨微服务架构是什么?有哪些优点和不足?

PostgreSQL 出现cross-database references are not implemented的bug

基于xlsx的B+树索引实现

【新功能】Ambire 钱包集成了 Metis 网络
![[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber](/img/92/57792ef733964230d36e0b9bb218b4.png)
[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber
![[fpga+sin] FPGA implementation of sinusoidal signal generator module based on DDS (direct digital synthesis)](/img/7d/d507d435fe97de005e20560fd6ba35.png)
[fpga+sin] FPGA implementation of sinusoidal signal generator module based on DDS (direct digital synthesis)

访问数据库时出现错误
随机推荐
Microsecond TCP timestamp
Hcie security day41: theoretical learning: information collection and network detection
Common methods of JS date and time
二叉树序列化与反序列化(leetcode(困难))
Yangzhou needs one English IT Helpdesk Engineer -20220216
Anaconda自带的Spyder编辑器启动报错问题
2022年 6月27号 《暑假感悟篇一》路程的选择权。
iNFTnews | 元宇宙技术将带来全新的购物体验
【FPGA+sin】基于DDS(直接数字合成)的正弦信号发生器模块FPGA实现
New listing of operation light 3.0 -- a sincere work of self subversion across the times
C language -- branch structure
Idea of importing point cloud map into gazebo
[Brillouin phenomenon] Study on simultaneous measurement system of Brillouin temperature and strain distribution in optical fiber
Data collection and management [15]
String differences between different creation methods
I came from a major, so I didn't want to outsource
Efficientnetv2 - get smaller models and faster training with NAS, scaling, and fused mbconv
logstash启动过慢甚至卡死
Web crawler knowledge day04
Cloud native weekly | grafana 9 was officially released; The Chinese version of cloud native vocabulary is now online