LeetCode85 Maximal Rectangle

LeetCode85 Maximal Rectangle

问题来源 LeetCode85

问题描述

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.

问题分析

这道题目其实和LeetCode84有很大的关系。比如题目给定的矩阵。

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

可以转化成

1 0 1 0 0
2 0 2 1 1
3 1 3 2 2
4 0 0 3 0

也就是把每列连续的1 加起来,形成新的列,而矩形的面积可以通过Rectangle in Histogram的规则计算出来。

比如第3行的 3 2 2 就是给定矩阵的最大矩形,也就是2 +2 +2 =6。

所以解决这道题的方法就是先把给矩阵的每一行转化,然后利用84题的算法进行计算,并保存最大值。

代码如下

public int maximalRectangle(char[][] matrix) {
    if(matrix==null|| matrix.length<1 ||matrix[0].length<1)
        return 0;
    int m =matrix.length;
    int n  = matrix[0].length;
    for(int i=1;i<m;i++){
        for (int j = 0; j < n; j++) {
            if(matrix[i][j]!='0'){
                matrix[i][j]=(char)(matrix[i][j]+matrix[i-1][j]-'0');
            }

        }
    }
    int max =0;
    for (int i = 0; i < m; i++) {
        max =Math.max(max,largestRectangleArea(matrix[i]));
    }
    return max;

}
public int largestRectangleArea(char[] heights) {
    if(heights==null ||heights.length<1){
        return 0;
    }
    int result = 0;
    Stack<Integer> stack = new Stack<>();
    stack.add(-1);
    for(int i=0;i<heights.length;i++){
        while(stack.size()!=1 && heights[stack.peek()]>=heights[i]){
            result = Math.max((heights[stack.pop()]-'0')*(i-stack.peek()-1),result);
        }
        stack.add(i);
    }
    int right = stack.peek();
    while (stack.size()>1){
        result  = Math.max((heights[stack.pop()]-'0')*(right-(stack.peek())),result);
    }
    return result;
}

LeetCode学习笔记持续更新

GitHub地址 https://github.com/yanqinghe/leetcode

CSDN博客地址 http://blog.csdn.net/yanqinghe123/article/category/7176678

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值