Code failing for some test cases


#1

public class Solution {
public int solve(int[][] A) {
int rows = A.length;
int cols = A[0].length;
int[][] dp = new int[rows][cols];
dp[0][0] = 1;
int m1 = 0;
int m2 = 0;
int max = -1;
if(rows==1 && cols==1)
return 1;

    for(int i=0; i<rows; i++){
        for(int j=0; j<cols; j++){
            if(i==0 && j==0)
                continue;
                
            if(i==0 && j!=0){
                if(A[i][j]>A[i][j-1] && dp[i][j-1]!=0)
                    dp[i][j] = dp[i][j-1] + 1;
            }
            else if(j==0 && i!=0){
                if(A[i][j]>A[i-1][j] && dp[i-1][j]!=0)
                    dp[i][j] = dp[i-1][j] + 1;
            }
            else{
                if(A[i][j]>A[i-1][j] && dp[i-1][j]!=0)
                    m1 = dp[i-1][j] + 1;
                if(A[i][j]>A[i][j-1] && dp[i][j-1]!=0)
                    m2 = dp[i][j-1] + 1;
                dp[i][j] = Math.max(m1, m2);
            }
            if(dp[i][j]!=0)
                max = Math.max(max, dp[i][j]);
        }
    }
    if(dp[rows-1][cols-1]==0)
        return -1;
    return max;
}

}