Beats 99% solution


  • 0
    B

    // for explanation refer this link below
    // https://www.youtube.com/watch?v=_Lf1looyJMU

    public class Solution {
    public int maximalSquare(char[][] matrix) {
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
    {
    return 0;
    }
    int mat[][] = new int[matrix.length+1][matrix[0].length+1];
    for(int i=0;i<mat.length;i++)
    {
    for(int j=0;j<mat[0].length;j++)
    {
    mat[i][j]=0;
    }
    }
    int max = 0;
    for(int i=0;i<matrix.length;i++)
    {
    for(int j=0;j<matrix[0].length;j++)
    {
    if(matrix[i][j]=='1')
    {
    mat[i+1][j+1] = Math.min(Math.min(mat[i][j],mat[i+1][j]),mat[i][j+1])+1;
    if (mat[i+1][j+1] > max)
    {
    max = mat[i+1][j+1];
    }
    }

        }
    }
    return max*max;
    }
    

    }


Log in to reply
 

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