当前位置:网站首页>leetcode:221. 最大正方形【dp状态转移的精髓】
leetcode:221. 最大正方形【dp状态转移的精髓】
2022-07-05 12:40:00 【白速龙王的回眸】

分析
dp[i][j]表示以(i, j)为右下角的最大正方形边长
第一行和第一列特殊
其他的话需要考虑精髓式子:
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
简直人间精华
ac code
class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
ans = 0
# dp[i][j]表示以(i, j)为右下角的最大全1正方形面积
m, n = len(matrix), len(matrix[0])
dp = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if matrix[i][j] == '1':
if i == 0 or j == 0:
dp[i][j] = 1
else:
# 精髓
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
ans = max(ans, dp[i][j] * dp[i][j])
return ans
总结
dp套路
关于找到一个全是1的最大正方形
边栏推荐
- jxl笔记
- RHCSA3
- 10 minute fitness method reading notes (5/5)
- SAP UI5 视图里的 OverflowToolbar 控件
- Alipay transfer system background or API interface to avoid pitfalls
- [cloud native] event publishing and subscription in Nacos -- observer mode
- NLP engineer learning summary and index
- 自然语言处理系列(一)入门概述
- SAP UI5 FlexibleColumnLayout 控件介绍
- Install rhel8.2 virtual machine
猜你喜欢

A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this

HiEngine:可媲美本地的云原生内存数据库引擎

激动人心!2022开放原子全球开源峰会报名火热开启!

谈谈我写作生涯的画图技巧

The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation

Volatile instruction rearrangement and why instruction rearrangement is prohibited

What is the difference between Bi software in the domestic market

Transactions from December 29, 2021 to January 4, 2022

Transactions from December 27 to 28, 2021

Simply take stock reading notes (4/8)
随机推荐
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
About LDA model
[cloud native] use of Nacos taskmanager task management
Free testing of Taobao tmall API order and flag insertion remark interface
Install rhel8.2 virtual machine
Reshape the power of multi cloud products with VMware innovation
SAP UI5 DynamicPage 控件介绍
Compilation principle reading notes (1/12)
由扫地增而引起的小叙
ActiveMQ installation and deployment simple configuration (personal test)
Simply take stock reading notes (1/8)
激动人心!2022开放原子全球开源峰会报名火热开启!
Principle of universal gbase high availability synchronization tool in Nanjing University
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
【云原生】Nacos-TaskManager 任务管理的使用
Kotlin变量
Transactions from December 27 to 28, 2021
Add a new cloud disk to Huawei virtual machine
Pytoch through datasets Imagefolder loads datasets directly from files
Transactions from January 14 to 19, 2022