Leetcode - Maximal Rectangle

《Leetcode - Maximal Rectangle》 Screenshot from 2016-01-15 17:47:02.png

My code:

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0)
            return 0;
        int[][] tracker = new int[matrix.length][matrix[0].length];
        /** initialize tracker[][] */
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == '1')
                    tracker[i][j] = 1;
            }
        }
        
        /** calculate area from up to down without consideration of nearing area */
        for (int i = 1; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (tracker[i][j] != 0) {
                    tracker[i][j] += tracker[i - 1][j]; 
                }
            }
        }
        /** find the maximum area */
        int max = 0;
        for (int i = 0; i < matrix.length; i++) {
            int maximumArea = largestRectangleArea(tracker[i]);
            max = Math.max(max, maximumArea);
        }
        return max;
    }
    /** get the maximal area in each line, using larest rectangle in histogram */
    private int largestRectangleArea(int[] height) {
        if (height == null || height.length == 0)       
            return 0;
        Stack<Integer> s = new Stack<Integer>();
        int[] helper = new int[height.length + 1];
        for (int i = 0; i < height.length; i++)
            helper[i] = height[i];
        int max = 0;
        int zeroLocation = -1;
        for (int i = 0; i < helper.length; i++) {
            if (s.isEmpty() || helper[i] >= helper[s.peek()]) { // insert into stack with increasing elem values
                s.push(i);
            }
            else {
                int end = i; // get the end index
                while (!s.isEmpty()) {
                    if (helper[s.peek()] >= helper[i]) { // if bigger, pop, area = value * (end - begin)
                        int index = s.pop();
                        int area = 0;
                        if (s.isEmpty()) {
                            int begin = zeroLocation;
                            area = helper[index] * (i - begin - 1);
                        }
                        else {
                            int begin = s.peek();
                            area = helper[index] * (i - begin - 1);
                        }
                        max = Math.max(max, area); // get the biggest area
                    }
                    else
                        break;
                }
                if (helper[i] > 0)
                    s.push(i); // push this index into stack
                else
                    zeroLocation = i;
            }
        }
        return max;
    }
}

这道题目之前做过,但是没有开文章,不知道怎么没写。
今天写了后思路很明确,就是叠加,然后用

  1. Largest Rectangle in Histogram
    http://www.jianshu.com/p/1cb8091863d5

作为辅助找到每一行(作为直方图来看)叠加在一起的最大面积。
这是解法的链接:
http://www.programcreek.com/2014/05/leetcode-maximal-rectangle-java/

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] map = new int[row][col];
        for (int i = 0; i < col; i++) {
            if (matrix[0][i] == '1') {
                map[0][i] = 1;
            }
        }
        
        for (int i = 1; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (matrix[i][j] == '0') {
                    map[i][j] = 0;
                }
                else {
                    map[i][j] = 1 + map[i - 1][j];
                }
            }
        }
        
        int max = 0;
        for (int i = 0; i < row; i++) {
            max = Math.max(max, getMaxHistogram(map[i]));
        }
        return max;
    }
    
    private int getMaxHistogram(int[] histogram) {
        if (histogram == null || histogram.length == 0) {
            return 0;
        }
        Stack<Integer> s = new Stack<Integer>();
        int[] h = new int[histogram.length + 1];
        for (int i = 0; i < histogram.length; i++) {
            h[i] = histogram[i];
        }
        int max = 0;
        for (int i = 0; i < h.length; i++) {
            if (s.isEmpty() || h[i] >= h[s.peek()]) {
                s.push(i);
            }
            else {
                while (!s.isEmpty() && h[i] < h[s.peek()]) {
                    int high = h[s.pop()];
                    int end = i;
                    int begin = (s.isEmpty() ? 0 : s.peek() + 1);
                    int area = high * (end - begin);
                    max = Math.max(max, area);
                }
                s.push(i);
            }
        }
        return max;
    }
}

差不多的想法, 用直方图最大面积那道题目作为基础。

DP solution:
My code:

public class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] dp = new int[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = col - 1; j >= 0; j--) {
                if (matrix[i][j] == '1') {
                    dp[i][j] = 1;
                    if (j < col - 1) {
                        dp[i][j] += dp[i][j + 1];
                    }
                }
            }
        }
        
        int max = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (max >= (row - i) * (col - j)) { // assume all 1 in this area, it is still smaller than max, then skip
                    continue;
                }
                else {
                    int min = Integer.MAX_VALUE;
                    for (int k = i; k < row; k++) {
                        if (dp[k][j] == 0) {
                            break;
                        }
                        min = Math.min(min, dp[k][j]);
                        int area = (k - i + 1) * min;
                        max = Math.max(max, area);
                    }
                }
            }
        }
        
        return max;
    }
}

reference:
http://www.cnblogs.com/TenosDoIt/p/3454877.html

DP的做法真的想不出来。。。
分析为什么想不出来。
因为受了那个求最大正方形面积题目的影响。
那个是从右下角开始,一步步累加出最优解。
但是这个题目,矩形是不可能累加的,因为我们还需要考虑到形状,朝向的问题。
那么,最直接的想法,从每个点开始算。
比如,计算以 (i, j) 为左上角的矩形的最大面积。
这个我们是可以做出来的。也就是,从第一行开始算。算出一个面积。
然后算第二行,第二行的宽必须是第一行和第二行宽的最小值。
然后算第三行。。。
这样下去,对于一个点,复杂度是 O(n ^ 2)
那么对于整体,复杂度就是 O(n ^ 4)

这就是brute-force解法。
然后想想有没有优化。那就是求宽度的时候,有些数据是可以cache的。
于是就可以用DP了,从右往左开始计算。
然后一切就顺理成章了。
然后加入一个剪枝,没想到效果这么好。。。
加入DP后,算法复杂度变成了 O(n ^ 3)
上面的使用直方图的解法,算法复杂度是 O(n ^ 2)
但是DP解法加入剪枝后,速度快了很多。

Anyway, Good luck, Richardo! — 08/18/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/c86e5dec7514#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞