首页 > 其他 > 详细

LeetCode 85: Maximal Recetangle

时间:2017-09-04 15:53:13      阅读:362      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/shuashuashua/p/7473769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!