Python Leetcode 221. Maximal Square 动态规划问题

这种题目肯定要用动态规划,像这种矩阵类型的,那么接下来就是找递推公式。
对于动态规划我也没有什么好的方法,就多做一点题吧,就有感觉了。
Done
class Solution(object):
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if len(matrix)==0 or len(matrix[0]) == 0:
return 0
maxside = 0
row, col = len(matrix), len(matrix[0])
dp = [[0] * col for _ in range(row)]
for i in range(row):
for j in range(col):
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
maxside=max(maxside,dp[i][j])
maxsquare = maxside * maxside
return maxsquare
LeetCode 221最大正方形DP解析

本文深入探讨LeetCode第221题“最大正方形”的动态规划解决方案,通过矩阵类型问题,详细讲解如何寻找递推公式,实现高效求解。并提供Python代码示例。
363

被折叠的 条评论
为什么被折叠?



