LeetCode 85: Maximal Recetangle

Note:

The lower one can cross other higher recetangle. Thus height[j] <= height[left/ right -/+ 1]

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

        int[] height = new int[matrix[0].length];
        int[] left = new int[matrix[0].length];
        int[] right = new int[matrix[0].length];
        int result = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == ‘1‘) {
                    height[j]++;
                } else{
                    height[j] = 0;
                }
            }

            for (int j = 0; j < matrix[i].length; j++) {
                left[j] = j;
                while (left[j] > 0 && height[j] <= height[left[j] - 1]) left[j] = left[left[j] - 1];
            }

            for (int j = matrix[i].length - 1; j >= 0; j--) {
                right[j] = j;
                while (right[j] < matrix[i].length - 1 && height[j] <= height[right[j] + 1]) right[j] = right[right[j] + 1];
            }

            for (int j = 0; j < matrix[i].length; j++) {
                result = Math.max(result, (right[j] - left[j] + 1) * height[j]);
            }
        }
        return result;
    }
}
时间: 2024-10-19 00:27:21

LeetCode 85: Maximal Recetangle的相关文章

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]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. Maximal Rectangle

1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char>>& matrix) { 4 /** largest rectangle based solution **/ 5 if(matrix.size()<=0 || matrix[0].size()<=0) 6 return 0; 7 int m=matrix.size(); 8 int n=matrix[0].size()+

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 ----- java

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. 求矩阵中由1组成的最大子矩阵面积. 1.首先进行观察,发现如果直接遍历然后求出每一

[LeetCode 85] Maximal Rectangle (华为2015机试)

题目链接:maximal-rectangle import java.util.Arrays; /** * Given a 2D binary matrix filled with 0's and 1's, * find the largest rectangle containing all ones and return its area. * */ public class MaximalRectangle { // 65 / 65 test cases passed. // Status

19.2.23 [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. Maximal Rectangle

85. Maximal Rectangle Hard 161653FavoriteShare 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"],

[leetcode] 85. 最大矩形

85. 最大矩形 解法1: 一个思路就是这个可以看作84. 柱状图中最大的矩形的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用 84. 柱状图中最大的矩形 中的方法,就可以得到最大的矩形面积. class Solution { public int maximalRectangle(char[][] matrix) { int m = matrix.length; if (m == 0) return 0; int n = ma