视频连接:https://www.bilibili.com/video/BV1po4y1d7C9/
class Solution:
def solve(self , matrix ):
if not matrix: return 0
rows = len(matrix)
cols = len(matrix[0])
dp = [[0 for _ in range(cols)] for _ in range(rows)]
res = 0
# 上边界或者左边界 最大只可能有 1的矩阵
for i in range(rows):
if matrix[i][0] == '1':
dp[i][0] = 1
for j in range(cols):
if matrix[0][j] == '1':
dp[0][j] = 1
for row in range(1, rows):
for col in range(1, cols):
if matrix[row][col] == '1':
# 从左、左上、上 三个方向 判断是否 能够组成 最大矩阵
dp[row][col] = min(dp[row-1][col], dp[row][col-1], dp[row-1][col-1]) + 1
# 更新 最大值
res = max(res, dp[row][col])
return res*res
京公网安备 11010502036488号