当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Maximal Rectangle

发表于: 2015-07-08   作者:hcx2013   来源:转载   浏览:
max
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.   public class Solution { public int maximalRectangle(char[][] matrix)

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

 

public class Solution {
    public int maximalRectangle(char[][] matrix) {
    	if (matrix==null || matrix.length==0 || matrix[0].length==0) {
    		return 0;
    	}
        int m = matrix.length;
        int n = matrix[0].length;
        int max = 0;
        int[] height = new int[n];
        for (int i = 0; i < m; i++) {
        	for (int j = 0; j < n; j++) {
        		if (matrix[i][j] == '0') {
        			height[j] = 0;
        		} else {
        			height[j] += 1;
        		}
        	}
        	max = Math.max(largestRectangleArea(height), max);
        }
        return max;
    }
    public int largestRectangleArea(int[] height) {
    	Stack<Integer> stack = new Stack<Integer>();
    	int i = 0;
    	int maxArea = 0;
    	int[] tmp = Arrays.copyOf(height, height.length+1);
    	while (i < tmp.length) {
    		if (stack.isEmpty() || tmp[stack.peek()] <= tmp[i]) {
    			stack.push(i++);
    		} else {
    			int t = stack.pop();
    			maxArea = Math.max(maxArea, tmp[t]*(stack.isEmpty() ? i: (i-stack.peek()-1)));
    		}
    	}
    	return maxArea;
    }
}

 

Maximal Rectangle

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
地址:https://oj.leetcode.com/problems/maximal-rectangle/ Given a 2D binary matrix filled wit
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all o
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all o
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl
相关问题1:[LeetCode] Find max subsquare whose border values are all 1 相关问题2:[LeetCode]
问题描述 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing
Description: Linda is a shopaholic. Whenever there is a discount of the kind where you can bu
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square cont
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is de
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号