LeetCode 85: Maximal Recetangle

Posted keepshuatishuati

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 85: Maximal Recetangle相关的知识,希望对你有一定的参考价值。

Note:

The lower one can cross other higher recetangle. Thus height[j] <= height[left/ right -/+ 1]

 

 

class Solution {
    public int maximalRectangle(char[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        
        int[] height = new int[matrix[0].length];
        int[] left = new int[matrix[0].length];
        int[] right = new int[matrix[0].length];
        int result = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                if (matrix[i][j] == ‘1‘) {
                    height[j]++;
                } else{
                    height[j] = 0;
                }
            }
            
            for (int j = 0; j < matrix[i].length; j++) {
                left[j] = j;
                while (left[j] > 0 && height[j] <= height[left[j] - 1]) left[j] = left[left[j] - 1];
            }
            
            for (int j = matrix[i].length - 1; j >= 0; j--) {
                right[j] = j;
                while (right[j] < matrix[i].length - 1 && height[j] <= height[right[j] + 1]) right[j] = right[right[j] + 1];
            }
            
            for (int j = 0; j < matrix[i].length; j++) {
                result = Math.max(result, (right[j] - left[j] + 1) * height[j]);
            }
        }
        return result;
    }
}

 

以上是关于LeetCode 85: Maximal Recetangle的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 85. Maximal Rectangle

Leetcode 85. Maximal Rectangle

LeetCode 85: Maximal Recetangle

LeetCode(85) Maximal Rectangle

leetcode 85. Maximal Rectangle

[leetcode]85. Maximal Rectangle 最大矩形