资讯专栏INFORMATION COLUMN

221. Maximal Square

freewolf / 2907人阅读

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

return 4
// O(mn) space
public class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix == null || matrix.length == 0) return 0;
        int m = matrix.length, n = matrix[0].length;
        // right-bottom corner of square, and its width
        int[][] dp = new int[m+1][n+1];
        int width = 0;
        for(int i=1; i<=m; i++){
            for(int j=1; j<=n; j++){
                if(matrix[i-1][j-1] == "1"){
                    dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) +1;
                    width = Math.max(width, dp[i][j]);
                } else dp[i][j] = 0;
            }
        }
        return width*width;
    }
}
// 只和上一层有关系,可以用O(n)空间,只记录上一层
public class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix == null || matrix.length == 0) return 0;
        int m = matrix.length, n = matrix[0].length;
        // right-bottom corner of square, and its width
        int[] dp1 = new int[n+1];
        int width = 0;
        for(int i=0; i           
               
                                           
                       
                 

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

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

相关文章

  • 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
  • [Leetcode] Maximal Square 最大正方形

    摘要:但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加了。假设表示以为右下角的正方形的最大边长,则有当然,如果这个点在原矩阵中本身就是的话,那肯定就是了。 Maximal Square Given a 2D binary matrix filled with 0s and 1s, find the larges...

    xiaowugui666 评论0 收藏0
  • [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
  • 85. Maximal Rectangel

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

    Corwien 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<