LeetCode 85. Maximal Rectangle(最大矩形)

原题网址:https://leetcode.com/problems/maximal-rectangle/

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 {
    private Stack stack = new Stack<>();
    private int histogram(int[] nums) {
        stack.clear();
        int max = 0;
        for(int j=0; j nums[j]);
            stack.push(j);
        }
        while (!stack.isEmpty()) {
            int p = stack.pop();
            int s = stack.isEmpty()? nums[p] * nums.length: nums[p] * (nums.length - stack.peek() - 1);
            max = Math.max(max, s);
        }
        return max;
    }
    public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int max = 0;
        int n = matrix.length;
        int m = matrix[0].length;
        int[][] counts = new int[n][m];
        for(int j=0; j


你可能感兴趣的:(直方图,矩形,面积,最值,矩阵)