Java Simple 364


  • 0
    R
    public class Solution {
        public int maximalSquare(char[][] matrix) {
                if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
            return 0;
        int[] height = new int[matrix[0].length];
        int res = 0;
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[0].length; j++)
                height[j] = matrix[i][j] == '1' ? height[j] + 1 : 0;
            res = Math.max(res, largest(height));
        }
        return res;
        }
        public int largest(int[] height) {
            if(height==null || height.length==0) return 0;
            Stack<Integer> s = new Stack<Integer>();
            int max = 0;
            for(int i=0;i<=height.length;i++){
                int cur = (i==height.length)? 0:height[i];
                if(s.empty() || cur>height[s.peek()])
                    s.push(i);
                else{
                    int tmp = s.pop();
                    max = (int) Math.max(max,Math.min(height[tmp]*height[tmp],Math.pow((s.empty()?i:i-s.peek()-1),2)));
                    i--;
                }
            }
            return max;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.