1. 程式人生 > >LeetCode 85: Maximal Recetangle

LeetCode 85: Maximal Recetangle

rect etc angle color note ++ nbsp char leetcode

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