当前位置:网站首页>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的最大正方形
边栏推荐
- mysql拆分字符串做条件查询
- View and terminate the executing thread in MySQL
- Distributed solution - distributed lock solution - redis based distributed lock implementation
- 《信息系统项目管理师》备考笔记---信息化知识
- SAP self-development records user login logs and other information
- Why is your next computer a computer? Explore different remote operations
- Transactions from January 6 to October 2022
- How do e-commerce sellers refund in batches?
- 你的下一台电脑何必是电脑,探索不一样的远程操作
- Reshape the power of multi cloud products with VMware innovation
猜你喜欢
What is the difference between Bi software in the domestic market
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Simply take stock reading notes (3/8)
石臻臻的2021总结和2022展望 | 文末彩蛋
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
Taobao short video, why the worse the effect
Four common problems of e-commerce sellers' refund and cash return, with solutions
Talk about my drawing skills in my writing career
我在滴滴做开源
UNIX socket advanced learning diary -ipv4-ipv6 interoperability
随机推荐
Notes for preparation of information system project manager --- information knowledge
Free testing of Taobao tmall API order and flag insertion remark interface
石臻臻的2021总结和2022展望 | 文末彩蛋
Alipay transfer system background or API interface to avoid pitfalls
Why is your next computer a computer? Explore different remote operations
Programming skills for optimizing program performance
JDBC -- extract JDBC tool classes
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
2021-12-21 transaction record
I met Tencent in the morning and took out 38K, which showed me the basic smallpox
NLP engineer learning summary and index
Distributed solution - completely solve website cross domain requests
View and modify the MySQL data storage directory under centos7
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
jxl笔记
Reshape the power of multi cloud products with VMware innovation
mysql拆分字符串做条件查询
Simply take stock reading notes (3/8)
SAP SEGW 事物码里的 Association 建模方式
Distributed solution - Comprehensive decryption of distributed task scheduling platform - xxljob scheduling center cluster