Java implementation LeetCode 85 largest rectangle

85. The largest rectangle

Given a two-dimensional binary matrix contains only 0 and 1, to find the largest rectangular contain only 1, and returns its area.

Example:

Input:
[
[ "1", "0", "1", "0", "0"],
[ "1", "0", "1", "1", "1"],
[ "1 "," 1 "," 1 "," 1 "," 1 "],
[" 1 "," 0 "," 0 "," 1 "," 0 "]
]
output: 6

PS:
monotonically method to solve the stack (the same 84)

class Solution {
      public int maximalRectangle(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        int[] height = new int[matrix[0].length];
        int globalmax = 0;
        for (int i = 0; i < matrix.length; i++){
            for (int j = 0; j < matrix[0].length; j++){
                if (matrix[i][j] == '0') height[j] = 0;
                else height[j]++;
            }
            globalmax = Math.max(globalmax, maxrow(height));
        }
        return globalmax;
    }
    public int maxrow(int[] height){
        Stack<Integer> st = new Stack<>();
        int localmax = 0;
        for (int i = 0; i <= height.length; i++){
            int h = (i == height.length)? 0 : height[i];
            while (!st.isEmpty() && height[st.peek()] >= h){
                int maxheight = height[st.pop()];
                int area = st.isEmpty()? i * maxheight : maxheight * (i - st.peek() -1);
                localmax = Math.max(localmax, area);
            }
            st.push(i);
        }
        return localmax;
    }
}
Released 1200 original articles · won praise 10000 + · views 570 000 +

Guess you like

Origin blog.csdn.net/a1439775520/article/details/104350525