TLE even when optimized


#1

public class Solution {
public int solve(int A, int B) {
int[][] memo=new int[A+1][B+1];
return helper(A,B,memo,0)%1000000007; // A is n digit number and B is the sum
}
private int helper(int n,int sum,int[][] memo,int len){
// len is to avoid the leading zero
if(n==0){
if(sum==0){
return 1;
}
return 0;
}
if(memo[n][sum]!=0){
return memo[n][sum];
}
int result=0;
for(int i=0;i<=9;i++){
if(i==0 && len==0){
continue;
}
if((sum-i)>=0){
result+=helper(n-1,sum-i,memo,len+1);
}
}
return memo[n][sum]=result%1000000007;
}
}