leetcode85 - Maximal Rectangle - hard

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

Iterative的largest rectangle in histogram.

思路:一层一层遍历,到i层时,第i层是1的位置可以向上延伸所有连续的1作为一个直方条条,按这样的规律可以把matrix[0:i][:]看做一个直方图,然后去统计当前情况下的最大rectangle,得到答案后去打擂台。所有层遍历完了,答案就出来了。

直方图的储存:用int[] heights[colLength]来储存,更新的方法是,扫matrix里新的一行时,如果看到’0’就清空heights[j],如果看到’1’就让heights[j]++。

解释计算直方图里的清空操作:直方条的定义是底部非空向上生长。因为histogram问题里能用stack解决的原因就是,所有直方图最底部开始都是非空的,那么算面积是可以只在意顶上高到哪里,不用担心底部有没有悬空,从而记录高度即可。如果你把matrix的局部转化成直方图的时候看到上面有1但底部是0,那这一列都不合格直方条的定义了。上面的1不用担心,你之前遍历到前面那行的时候算过了。

相关题目:Largest Rectangle in Histogram。  https://www.cnblogs.com/jasminemzy/p/9764297.html

实现:

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

        int ans = 0;
        int[] heights = new int[matrix[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == ‘0‘) {
                    heights[j] = 0;
                } else {
                    heights[j]++;
                }
            }
            ans = Math.max(ans, maxRecInHistogram(heights));
        }
        return ans;
    }

    private int maxRecInHistogram(int[] heights) {
        int ans = 0;
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i <= heights.length; i++) {
            while (!stack.isEmpty() && (i == heights.length || heights[i] < heights[stack.peek()])) {
                int height = heights[stack.pop()];
                int width = stack.isEmpty() ? i : i - stack.peek() - 1;
                ans = Math.max(ans, height * width);
            }
            stack.push(i);
        }
        return ans;
    }
}

原文地址:https://www.cnblogs.com/jasminemzy/p/9770341.html

时间: 2024-10-31 13:54:17

leetcode85 - Maximal Rectangle - hard的相关文章

LeetCode85 Maximal Rectangle java题解

public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return 0; int columnNum=matrix[0].length; int[][] height=new int[rowNum][columnNum+1]; int maxarea=0; for(int i=0;i<rowNum;i++) { for(int j=0;j<columnNum;j

Maximal Rectangle

题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 方法 使用两个矩阵,分别记录每一行连续的1的个数以及每一列连续的1的个数. public int maximalRectangle(char[][] matrix) { int lenX = matrix.length; if (lenX == 0) { r

LeetCode: Maximal Rectangle

LeetCode: Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为

Leetcode:Maximal Rectangle 最大全1子矩阵

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 解题分析: 联想到 最大矩形面积 这一题,可以在O(n)时间内求出 最大的矩形面积 如果我们把每一行看成x坐标,那高度就是从那一行开始往上数的1的个数. 利用 最大矩形面积 的方法,在O(n2)时间内就可以求出每一行形成的“柱状

【leetcode】Maximal Rectangle

Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

【leetcode刷题笔记】Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解,很巧妙的一道题,对于一个0-1矩阵,它的每一行及以上都可以看作一个直方图(如下图所示),利用Largest Rectangle in Histogram的方法,可以在O(n)的时间搜索出这一行及以上的直方图中面积最大的矩形,对矩阵的每一行依次做这个操作,就可

Maximal Rectangle leetcode java

题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 题解: 这道题可以应用之前解过的Largetst Rectangle in Histogram一题辅助解决.解决方法是: 按照每一行计算列中有1的个数,作为高度,当遇见0时,这一列高度就为0.然后对每一行计算 Largetst Rectangle in H

[LeetCode] Maximal Rectangle(good)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法: class Solution { public: int maximalRectang