[leetcode] 85. 最大矩形

85. 最大矩形

解法1:

一个思路就是这个可以看作84. 柱状图中最大的矩形的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用 84. 柱状图中最大的矩形 中的方法,就可以得到最大的矩形面积。

class Solution {
    public int maximalRectangle(char[][] matrix) {
        int m = matrix.length;
        if (m == 0) return 0;
        int n = matrix[0].length;
        if (n == 0) return 0;

        int[] height = new int[n];
        int ans = 0;

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == ‘0‘) height[j] = 0;
                else if (matrix[i][j] == ‘1‘) height[j] += 1;
            }
            ans = Math.max(largestRectangleArea(height), ans);
        }
        return ans;
    }

    public int largestRectangleArea(int[] heights) {
        if (heights.length == 0) return 0;
        Stack<Integer> stack = new Stack<>();

        int max = 0;
        for (int i = 0; i < heights.length; i++) {
            while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
                int tmp = stack.pop();
                // 把当前的tmp木板作为最短木板,看能组成的最大面积是多少
                max = Math.max(max, heights[tmp] * (stack.empty() ? i : i - stack.peek() - 1));
            }
            stack.push(i);
        }

        int tmp = 0;
        int len = heights.length;
        while (!stack.isEmpty()) {
            tmp = stack.pop();
            max = Math.max(max, heights[tmp] * (stack.empty() ? len : len - stack.peek() - 1));
        }

        return max;
    }
}

解法2:

另外一个思路就是,想办法如何很方便的表示出一个矩形。比如说,对于以(i,j)点为右下角的矩形。

大家可以先想暴力思路,实际上大部分的优化思路,都是以暴力思路基础而来的。

暴力思路:枚举(i,j)为一个矩形的右下角,然后以(i,j)为起点,向左、向上看,能组成的最大矩形是什么。时间复杂度为O(n^4)。

仔细观察一下,其实在向左、向上看,能组成的最大矩形是什么这个过程中,是存在重复计算的。也就是说当我们算完点(i,j)后,(i+1,j)点,我们或许只需要看第i+1行就可以了。

所以,我们可否用动态规划来做呢?

有个思路很巧妙:

height数组和上面一样,height表示高,这里的left数组表示左边界是1的位置,right数组表示右边界是1的位置,那么对于任意一行的第j个位置,矩形为(right[j] - left[j]) * height[j]

具体参考:算法学习之动态规划 85. Maximal Rectangle

文章里讲的方法我大概是懂了,其核心思路是从第一行开始一行一行地处理,然后其left与right数组巧妙的保存了上层的状态,使得其能表示出矩阵左边界或者右边界的位置,进而就很方便的表示出了一个矩形了

但是,我觉得我是想不出来这个思路的- -。。非常的巧妙啊

原文地址:https://www.cnblogs.com/acbingo/p/9398981.html

时间: 2024-08-26 17:23:55

[leetcode] 85. 最大矩形的相关文章

[leetcode 85. 最大矩形] 单调栈--84题的简单扩展

题目描述 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","

[leetcode]85. Maximal Rectangle 最大矩形

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",&qu

【LeetCode 85】最大矩形(第二遍)

题目链接 [题解] 首先 我们处理出来一个数组 a[i][j]. 这个数组的含义是,矩阵中(i,j)包括自身往上有多少个连续的1. 然后我们枚举行i. 表示我们现在要考察的矩阵的下边在第i行. 然后我们再处理出来一个一维数组heights[j] 其中heights[j] = a[i][j] 然后,问题就转化为在一个柱状图里面求一个最大的矩形了.用这个方法 做就行了. 枚举行O(N)的复杂度,柱状图求最大的矩阵也是O(N)的复杂度 因此这道题的时间复杂度为O(N^2) [代码] class Sol

LeetCode (85): Maximal Rectangle [含84题分析]

链接: https://leetcode.com/problems/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','1','1','0'], ['1','1','1','1']

[LeetCode] Rectangle Area 矩形面积

Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. Cre

leetcode 签到 836. 矩形重叠

836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示

leetCode 85.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的矩阵,求所有为1组成的最大矩阵的面积. 此题能够巧妙转化为求最大直方图面积的问题. public class Solution { //其思想是将每一列的1逐行相加,遇0为0.遇1相加 //然后转化为求每一行的最大直方图面积的求解

leetcode || 85、Maximal Rectangle

problem: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. Hide Tags Array Hash Table Stack Dynamic Programming 题意:在一个由0.1组成的矩阵中寻找全是1的最大矩形,返回其面积 thinking: 这一题是上一条的变形,具体参考 http://blog

[LeetCode] Rectangle Overlap 矩形重叠

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b