Simple DFS Solution for the problem :Solved


#1

public class Solution {
int max_count=0;
public void dfs(int [][] A, int count,int i,int j)
{

    if (i==A.length-1 && j==A[0].length-1)
    {
        max_count=Math.max(max_count,count);
    }
    else if (i+1>=0 && i+1<A.length && j>=0 && j<A.length && A[i][j]<A[i+1][j])
    {
        dfs(A,count+1,i+1,j);
    }
    else if(i>=0 && i<A.length && j+1>=0 && j+1<A.length && A[i][j]<A[i][j+1])
    {
         dfs(A,count+1,i,j+1);
    }
    A[i][j]=0;
     return;
    
}
public int solve(int[][] A) {
 
 dfs(A,1,0,0);
 if (max_count==0) return -1;
 return max_count;
    
}

}