What is wrong in this logic


#1

Below is my code :
int MOD=1000000007;
public int solve(int A, int B) {
int dp[][] = new int[A+1][B+1];

    for(int i=0;i<=A;i++)
      Arrays.fill(dp[i],0);
      
    for(int i=1;i<=A;i++){
        for(int j=1;j<=B;j++){
            if(i==1) dp[i][j]=1;
            else{
               dp[i][j]= (dp[i-1][j]%MOD+dp[i][j-1]%MOD)%MOD;
            }
        }
    }
    return dp[A][B];
}

#2

I have written similar logic, did you find what is wrong with your logic?
Thanks.


#3

No, I didn’t able to find the loop hole in this…


#4

Use the below code and print the dp table for (sum = 12, n = 12),
then compare with our code;
you will see the difference and understand what is wrong with our approach.

    if s < 1 or s > 9*n :
        return 0
    mod = 1000000007
    dp = [[-1 for i in range(s)] for i in range(n)] 
    
    for i in range(s) :
        if i+1 <= 9 :
            dp[0][i] = 1
        else :
            dp[0][i] = 0
    for j in range(n) :
        dp[j][0] = 1

    
    for i in range(1, n) :
        for j in range(1, s) :
            dp[i][j] = 0
            k = 0
            if j>= 9 :
                k = j-9
            for t in range(k, j+1) :
                dp[i][j] = (dp[i][j] + dp[i-1][t])%mod
                
    return dp[n-1][s-1]