资讯专栏INFORMATION COLUMN

85. Maximal Rectangel

Corwien / 2427人阅读

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

题目:
Given a 2D binary matrix filled with 0"s and 1"s, find the largest rectangle containing all ones and return its area.

解答:
这题思路很重要,一定要理清previous row和current row的参数之间的关系,那么就事半功倍了。left[]表示从左往右到i,出现连续‘1’的string的第一个座标,right[]表示从右往左到i, 出现连续‘1’的string的最后一个座标,height[]表示从上到下的高度。那么用(left[j] - right[j] + 1)(横长) * height[j]就是可能的最大的矩形了。

public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int max = 0;
        int m = matrix.length, n = matrix[0].length;
        int[] left = new int[n];
        int[] right = new int[n];
        int[] height = new int[n];
        Arrays.fill(left, 0);
        Arrays.fill(height, 0);
        Arrays.fill(right, n - 1);
        
        for (int i = 0; i < m; i++) {
            int curLeft = 0, curRight = n - 1;
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == "1") height[j]++;
                else height[j] = 0;
            }
            //1 1 0 1
            //1 1 0 1
            //1 1 1 1
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == "1") {
                    //见上述例子,left[j]保证了前面的数组是正方形且没有0的最小矩形,
                    //All the 3 variables left, right, and height can be determined by the information from previous row, and also information from the current row.
                    left[j] = Math.max(left[j], curLeft);
                } else {
                    left[j] = 0;
                    curLeft = j + 1;
                }
            }
            for (int j = n - 1; j >= 0; j--) {
                if (matrix[i][j] == "1") {
                    right[j] = Math.min(right[j], curRight);
                } else {
                    right[j] = n - 1;
                    curRight = j - 1;
                }
            }
            for (int j = 0; j < n; j++) {
                max = Math.max(max, (right[j] - left[j] + 1) * height[j]);
            }
        }
        return max;
    }

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

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

相关文章

  • leetcode85. Maximal Rectangle

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

    jhhfft 评论0 收藏0
  • [leetcode]85. Maximal Rectangle

    摘要:对于一个矩形,可以用最高可能的高度来唯一标记该矩形。剩下的宽度由该最高高度所表示矩形的最左边界和最右边界得出。 Given a 2D binary matrix filled with 0s and 1s, find the largest rectangle containing only 1s and return its area.For example, given the ...

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

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

    xiaowugui666 评论0 收藏0
  • 221. Maximal Square

    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; ...

    freewolf 评论0 收藏0

发表评论

0条评论

Corwien

|高级讲师

TA的文章

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