[leetcode]Maximal Rectangle

来源:互联网 发布:ubuntu查看硬盘大小 编辑:程序博客网 时间:2024/06/04 20:01

from : 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:

遍历i,j,当这个位置为1,则以之为矩形左上角,遍历以这个点为左上角的矩形,更新最大面积。

public class Solution {    public int maximalRectangle(char[][] matrix) {        if(matrix==null || matrix.length==0) {            return 0;        }        int max = 0;        int m=matrix.length, n=matrix[0].length;        boolean[][] isOne = new boolean[m][n];                for(int I=0; I<m; ++I) {            for(int J=0; J<n; ++J) {                if( isOne[I][J] = (matrix[I][J]=='1') ) {                    // I, J is '1'                    for(int i=I; i<m; ++i) {                        for(int j=J; j<n; ++j) {                            // find boundry                            if(i == I && j == J) {                                // continue;                            } else if(i == I) {                                isOne[i][j] = isOne[i][j-1] && (matrix[i][j] == '1');                            } else if(j == J) {                                isOne[i][j] = isOne[i-1][j] && (matrix[i][j] == '1');                             } else {                                isOne[i][j] = isOne[i-1][j] && isOne[i][j-1] && matrix[i][j] == '1';                            }                            if(isOne[i][j]) {                                int a = (i-I+1)*(j-J+1);                                max = a>max ? a : max;                            } else {                                break;                            }                        }                    }                }                            }        }                return max;    }}


思路2:

借用leetcode的Largest Rectangle in Histogram。求出每行下面的直方图,然后计算。解法见Largest Rectangle in Histogram。

public class Solution {    private Stack<Integer> idxes = new Stack<Integer>();    public int maximalRectangle(char[][] matrix) {if (matrix.length == 0 || matrix[0].length == 0) {return 0;}int m = matrix.length;int n = matrix[0].length;int max = 0;int[][] grid = new int[m][n];for(int i=0; i<m; ++i) {    for(int j=0; j<n; ++j) {        int k=i;        while(k < m && '1' == matrix[k][j]) {            k++;        }        grid[i][j] = k-i;    }}for(int i=0; i<m; ++i) {    max = Math.max(maxHistogram(grid[i]), max);}return max;    }    private int maxHistogram(int[] height) {        int max = 0, len=height.length;        for(int i=0; i<len;) {            if(idxes.empty() || height[idxes.peek()] <= height[i]) {                idxes.push(i);                ++i;            } else {                int idx = idxes.pop();                int width = idxes.empty()? i : i-1-idxes.peek();                max = Math.max(max, width*height[idx]);            }        }        while(!idxes.empty()) {            int idx = idxes.pop();            int width = idxes.empty()? len : len-1-idxes.peek();            max = Math.max(max, width*height[idx]);        }        return max;    }}


0 0
原创粉丝点击