221. Maximal Square

221. Maximal Square

import numpy as np
class Solution:
    def maximalSquare(self, matrix: List[List[str]]) -> int:
        rows,cols=len(matrix),len(matrix[0])

        dp=np.zeros((rows+1,cols+1))

        ans=0
        # print(dp.shape)

        for r in range(rows):
            for c in range(cols):
                if matrix[r][c]=='1':
                    # print(r,c)
                    dp[r+1][c+1]=min(dp[r][c],dp[r+1][c],dp[r][c+1])+1
                    ans=max(ans,dp[r+1][c+1])
        return int(ans*ans)

三种状态转移到当前状态

你可能感兴趣的:(leetcode)