[LeetCode] 221. 最大正方形(DP)

题目

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

示例:

输入:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

输出: 4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximal-square
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

  • dp[i][j] 表示以matrix[i-1][j-1]为右下角的最大正方形的边长
  • 转移方程:dp[i][j]=1+min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1]) 即除此点外将覆盖完全的最大正方形
  • 使用滚动数组减少一维空间,空间复杂度O(n)
  • 之所以dp[i][j]表示matrix[i-1][j-1],是免去了边界特例的判断,初始化自然为0
  • 注意二维数组对于数组长度为0的判断也是十分必要的。

代码

class Solution {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0) { //
            return 0;
        }

        int rows = matrix.length;
        int cols = matrix[0].length;
        int[] preDp = new int[cols + 1];
        int[] curDp = new int[cols + 1];

        int maxLen = 0;
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= cols; ++j) {
                if (matrix[i - 1][j - 1] == '1') {
                    curDp[j] = 1 + Math.min(preDp[j - 1], Math.min(preDp[j], curDp[j - 1]));
                    if (curDp[j] > maxLen) {
                        maxLen = curDp[j];
                    }
                }
            }
            for (int j = 1; j <= cols; ++j) {// 更新两个dp数组
                preDp[j] = curDp[j];
                curDp[j] = 0;
            }
        }
        return maxLen * maxLen;
    }
}

原文地址:https://www.cnblogs.com/coding-gaga/p/11741284.html

时间: 2024-10-02 21:12:46

[LeetCode] 221. 最大正方形(DP)的相关文章

[leetcode] 221. 最大正方形

221. 最大正方形 其实这题是85. 最大矩形的特殊情况,我们将85题代码稍微改一下,然后直接套用即可. 此题要求是正方形,那么我们在计算长与宽时,取短的那条然后平方即可. class Solution { public int maximalSquare(char[][] matrix) { return maximalRectangle(matrix); } public int maximalRectangle(char[][] matrix) { int m = matrix.leng

[LeetCode] Interleaving String(dp)

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 分析:非常好的DP的训练题,

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

[LeetCode] Distinct Subsequences(DP)

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

[LeetCode] Decode Ways(DP)

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

[LeetCode] Unique Paths II(DP)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 现在考虑网格中有障碍物.那么从左上角到右下角将会有多少条不同的路径? 网格中的障碍物和空位置分别用 1 和 0 来表示. 说明:m 和 n 的值均不超过 100. 示例 1: 输入: [   [0,0,0],  

(DP 线性DP) leetcode 221. Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 =============================================================== 这是一个D

[LeetCode] 221. Maximal Square 最大正方形

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. Credits:Special thanks to @Freezen for addin