Hard-题目30:85. Maximal Rectangle

原创 2016年05月31日 23:42:26

题目原文:
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矩阵。
题目分析:
(这也是原创的)朴素解法就是dfs,复杂度貌似为O(4n)就不谈了。
然后考虑DP,注意本题在Leetcode的题号是85题,好眼熟啊。刚才做到Hard-题目27:84. Largest Rectangle in Histogram是求直方图的最大面积。好像找到了什么关联对不对?没错,我们可以把这个0-1矩阵的第i行以上的子矩阵看做一个直方图,那么求每个子矩阵的直方图的面积最大值的最大值(好绕,看懂了么)就是题目的要求啦。
那么就好办了,直接调用Hard第27题的代码就行了。
源码:(language:java)
public class Solution {
public int maximalRectangle(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;

    int[] height = new int[matrix[0].length];
    for(int i = 0; i < matrix[0].length; i ++){
        if(matrix[0][i] == '1') height[i] = 1;
    }
    int result = largestInLine(height);
    for(int i = 1; i < matrix.length; i ++){
        resetHeight(matrix, height, i);
        result = Math.max(result, largestInLine(height));
    }

    return result;
}

private void resetHeight(char[][] matrix, int[] height, int idx){
    for(int i = 0; i < matrix[0].length; i ++){
        if(matrix[idx][i] == '1') height[i] += 1;
        else height[i] = 0;
    }
}    

public int largestInLine(int[] height) {
    if(height == null || height.length == 0) return 0;
    int len = height.length;
    Stack<Integer> s = new Stack<Integer>();
    int maxArea = 0;
    for(int i = 0; i <= len; i++){
        int h = (i == len ? 0 : height[i]);
        if(s.isEmpty() || h >= height[s.peek()]){
            s.push(i);
        }else{
            int tp = s.pop();
            maxArea = Math.max(maxArea, height[tp] * (s.isEmpty() ? i : i - 1 - s.peek()));
            i--;
        }
    }
    return maxArea;
}

}
成绩:
27ms,64.32%,32ms,8.41%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

LeetCode题目85.Maximal Rectangle(Hard)

题目Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones a...

LeetCode 85. Maximal Rectangle(Hard)

Maximal Rectangle(Hard)Description Given a 2D binary matrix filled with 0’s and 1’s, find the large...

【一天一道LeetCode】#85. Maximal Rectangle

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目...

【leetcode】Array——Maximal Rectangle(85)

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

[LeedCode OJ]#85 Maximal Rectangle

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:libin493073668@sina.com】 题目链接:https://leetcode.com/problems...

leetcode-85. Maximal Rectangle

leetcode-85. Maximal Rectangle题目: 针对每一个点计算从该点看到的最左边和最右边的边缘以及高度,这样()右边-左边)*高度就是面积。对每个点算面积就可以得到结果。问题在...

leetcode85 maximal rectangle

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and...

leetcode_question_85 Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones an...
  • doc_sgl
  • doc_sgl
  • 2013年09月19日 17:29
  • 14159

Leetcode:85. Maximal Rectangle

Difficulty: Hard Ac 44msGiven a 2D binary matrix filled with 0’s and 1’s, find the largest rectangl...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目30:85. Maximal Rectangle
举报原因:
原因补充:

(最多只允许输入30个字)