Share my Java AC solution


  • 1
    N
    public class Solution {
        public int maximalSquare(char[][] matrix) {
            if(matrix == null || matrix.length == 0) return 0;
            int x = matrix.length, y = matrix[0].length;
            int[][] m = new int[x][y];
            int max = 0;
            for(int i = 0; i < x; i++){
                for(int j = 0; j < y; j++){
                    if(i == 0 || j == 0) m[i][j] = matrix[i][j] - '0';
                    else if(matrix[i][j] == '0'){
                        m[i][j] = 0;
                    }else{
                        int min = Math.min(m[i - 1][j - 1], Math.min(m[i - 1][j], m[i][j - 1]));
                        m[i][j] = (int)Math.pow(((int)Math.sqrt(min) + 1), 2); 
                    }
                    if(m[i][j] > max) max = m[i][j];
                }
            }
            return max;
        }
    }

Log in to reply
 

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