leetcode-85. Maximal Rectangle

来源:互联网 发布:贵州广电网络9频道 编辑:程序博客网 时间:2024/06/18 04:08

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.

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.

这题综合答案来看有两种思路先说第一种

针对每一个点计算从该点看到的最左边和最右边的边缘以及高度,这样()右边-左边)*高度就是面积。对每个点算面积就可以得到结果。问题在于如果计算从每个点看到的最大矩形。
这里使用动态规划的方法,维护三个变量数组,以第一行为基础,计算到最后一行。
比如对于
0 0 0 1 0 0 0;
0 0 1 1 1 0 0;
0 1 1 1 1 1 0;

计算第一行的时候h,l,r分别为

0 0 0 1 0 0 0;
0 0 0 3 0 0 0;
7 7 7 4 7 7 7;

计算第二行的时候h,l,r分别为

0 0 1 2 1 0 0;
0 0 2 3 2 0 0;
7 7 5 4 5 7 7;

计算第三行的时候h,l,r分别为

0 1 2 3 2 1 0;
0 1 2 3 2 1 0;
7 6 5 4 5 6 7;

其中0和7都是为了简化计算而使用的,没有特殊含义。
这种解法效率比较高一些。O(m+n)
mn分别为行列长度

答案来源

public class Solution {    public int maximalRectangle(char[][] matrix) {        if(matrix==null || matrix.length==0 || matrix[0].length==0) return 0;        int [] l = new int [matrix[0].length];        int [] r = new int [matrix[0].length];        int [] h = new int [matrix[0].length];        Arrays.fill(r,matrix[0].length);        int ret = 0;        for(int i = 0 ; i < matrix.length; i++){            int cl =0 , cr = matrix[0].length;            for(int j = 0 ; j < matrix[0].length ; j++){                if(matrix[i][j]=='1') h[j] = h[j]+1;                else h[j] = 0;            }            for(int j = 0 ; j < matrix[0].length ; j++){                if(matrix[i][j]=='1') l[j] = Math.max(l[j],cl);                else {                    l[j]=0;                    cl = j+1;                }            }            for(int j = matrix[0].length-1 ; j>=0 ;j--){                if(matrix[i][j]=='1') r[j] = Math.min(r[j],cr);                else{                    r[j] = matrix[0].length;                    cr = j;                }            }            for(int j = 0 ; j < matrix[0].length ; j++){                ret= Math.max(ret,(r[j]-l[j])*h[j]);            }        }        return ret;    }}

第二种解法是使用栈。基本思路来源就是84题。我们可以这样想想,从每一行来看。每一行对应的矩阵的高度其实就相当于是当前行的直方图,也就相当于求直方图中最大面积。这样一来就和84解法一样了。其实按照leetcode给的提示来看,这种解法是官方希望的解法,但实际上运行效率低于第一种。但也是非常漂亮的解法
答案来源

public class Solution {    public int maximalRectangle(char[][] matrix) {        if(matrix==null || matrix.length==0 || matrix[0].length==0) return 0;        int [] l = new int [matrix[0].length];        int [] r = new int [matrix[0].length];        int [] h = new int [matrix[0].length];        Arrays.fill(r,matrix[0].length);        int ret = 0;        for(int i = 0 ; i < matrix.length; i++){            int cl =0 , cr = matrix[0].length;            for(int j = 0 ; j < matrix[0].length ; j++){                if(matrix[i][j]=='1') h[j] = h[j]+1;                else h[j] = 0;            }            for(int j = 0 ; j < matrix[0].length ; j++){                if(matrix[i][j]=='1') l[j] = Math.max(l[j],cl);                else {                    l[j]=0;                    cl = j+1;                }            }            for(int j = matrix[0].length-1 ; j>=0 ;j--){                if(matrix[i][j]=='1') r[j] = Math.min(r[j],cr);                else{                    r[j] = matrix[0].length;                    cr = j;                }            }            for(int j = 0 ; j < matrix[0].length ; j++){                ret= Math.max(ret,(r[j]-l[j])*h[j]);            }        }        return ret;    }}
0 0
原创粉丝点击