*LeetCode 221 最大正方形(中等)

在一个由 '0''1' 组成的二维矩阵内,找到只包含 '1' 的最大正方形,并返回其面积。

示例:

输入:
matrix = [["1","0","1","0","0"],
          ["1","0","1","1","1"],
          ["1","1","1","1","1"],
          ["1","0","0","1","0"]]

输出:4

思路与代码

动态规划

dp[i][j]表示以 (i,j)(为右下角,且只包含 '1' 的正方形的边长最大值。

核心是dp(i,j)=min(dp(i−1,j),dp(i−1,j−1),dp(i,j−1))+1

证明过程来自LeetCode

*LeetCode 221 最大正方形(中等)_第1张图片
*LeetCode 221 最大正方形(中等)_第2张图片

class Solution {
   
    public int maximalSquare(char

你可能感兴趣的:(#,LeetCode,热题,HOT,100,算法,动态规划)