码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 85: Maximal Recetangle

时间:2017-09-04 16:01:23      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:length   note   span   turn   one   height   rectangle   other   char   

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

标签:length   note   span   turn   one   height   rectangle   other   char   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!