资讯专栏INFORMATION COLUMN

[Leetcode] Maximal Square 最大正方形

xiaowugui666 / 2617人阅读

摘要:但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加了。假设表示以为右下角的正方形的最大边长,则有当然,如果这个点在原矩阵中本身就是的话,那肯定就是了。

Maximal Square

Given a 2D binary matrix filled with 0"s and 1"s, find the largest square containing all 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.

动态规划 复杂度

时间 O(MN) 空间 O(MN)

思路

当我们判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况是是它的上方,左方和左上方为右下角的正方形的大小都一样的,这样加上该点就可以构成一个更大的正方形。但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加1了。假设dpi表示以i,j为右下角的正方形的最大边长,则有

dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1

当然,如果这个点在原矩阵中本身就是0的话,那dpi肯定就是0了。

代码
public class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix.length == 0) return 0;
        int m = matrix.length, n = matrix[0].length;
        int max = 0;
        int[][] dp = new int[m][n];
        // 第一列赋值
        for(int i = 0; i < m; i++){
            dp[i][0] = matrix[i][0] - "0";
            max = Math.max(max, dp[i][0]);
        }
        // 第一行赋值
        for(int i = 0; i < n; i++){
            dp[0][i] = matrix[0][i] - "0";
            max = Math.max(max, dp[0][i]);
        }
        // 递推
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = matrix[i][j] == "1" ? Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1 : 0;
                max = Math.max(max, dp[i][j]);
            }
        }
        return max * max;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64496.html

相关文章

  • [LintCode/LeetCode] Maximal Square

    摘要:类似这种需要遍历矩阵或数组来判断,或者计算最优解最短步数,最大距离,的题目,都可以使用递归。 Problem Given a 2D binary matrix filled with 0s and 1s, find the largest square containing all 1s and return its area. Example For example, given t...

    Drinkey 评论0 收藏0
  • leetcode85. Maximal Rectangle

    摘要:题目要求输入一个二维数组,其中代表一个小正方形,求找到数组中最大的矩形面积。思路一用二维数组存储临时值的一个思路就是通过存储换效率。从而省去了许多重复遍历,提高效率。这里我使用两个二维数组来分别记录到为止的最大长度和最大高度。 题目要求 Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle ...

    jhhfft 评论0 收藏0
  • [Leetcode] Largest Rectangle (in Histogram) 最大矩形

    摘要:以此类推,如果一直到栈为空时,说明刚出来的竖条之前的所有竖条都比它自己高,不然不可能栈为空,那我们以左边全部的宽度作为长方形的宽度。 Largest Rectangle in Histogram Given n non-negative integers representing the histograms bar height where the width of each bar...

    邹强 评论0 收藏0
  • 221. Maximal Square

    摘要:题目解答第一眼看这道题以为是个搜索问题,所以用解了一下发现边界并没有办法很好地限定成一个,所以就放弃了这个解法。 题目:Given a 2D binary matrix filled with 0s and 1s, find the largest square containing all 1s and return its area. For example, given the ...

    lanffy 评论0 收藏0
  • 85. Maximal Rectangel

    摘要:题目解答这题思路很重要,一定要理清和的参数之间的关系,那么就事半功倍了。表示从左往右到,出现连续的的第一个座标,表示从右往左到出现连续的的最后一个座标,表示从上到下的高度。见上述例子,保证了前面的数组是正方形且没有的最小矩形, 题目:Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle co...

    Corwien 评论0 收藏0

发表评论

0条评论

阅读需要支付1元查看
<