资讯专栏INFORMATION COLUMN

[leetcode]85. Maximal Rectangle

stackvoid / 298人阅读

摘要:对于一个矩形,可以用最高可能的高度来唯一标记该矩形。剩下的宽度由该最高高度所表示矩形的最左边界和最右边界得出。

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

对于一个矩形,可以用最高可能的高度来唯一标记该矩形。剩下的宽度由该最高高度所表示矩形的最左边界和最右边界得出。
height:
1 0 1 0 0
2 0 2 1 1
3 1 3 2 2
4 0 0 3 0

left:
0 0 2 0 0
0 0 2 2 2
0 0 2 2 2
0 0 0 3 0

right:
1 5 3 5 5
1 5 3 5 5
1 5 3 5 5
1 5 5 4 5

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        int m = matrix.length;
        if(matrix == null || m == 0) return 0;
         int n = matrix[0].length;
        int maxA = 0;
        int[] right = new int[n], left = new int[n], heights = new int[n];  // rectangle with highest height
        Arrays.fill(right,n);
        
        for(int i=0; i=0;j--) {
                if(matrix[i][j] == "1") right[j] = Math.min(curright, right[j]);
                else{ right[j] = n; curright = j; }         // remain at last zero position
            }
            for(int j=0; j           
               
                                           
                       
                 

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

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

相关文章

  • 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
  • [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
  • [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

发表评论

0条评论

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