diff --git a/Number_of_Dice_Rolls_With_Target_Sum.cpp b/Number_of_Dice_Rolls_With_Target_Sum.cpp new file mode 100644 index 0000000..cca71f0 --- /dev/null +++ b/Number_of_Dice_Rolls_With_Target_Sum.cpp @@ -0,0 +1,21 @@ +#define M 1000000007 +class Solution { +public: + long long int solve(int n, int k, int x, vector>& dp){ + if(x<0) return 0; + if(n==0 && x!=0) return 0; + if(n!=0 && x==0) return 0; + if(n==0 && x==0) return 1; + if(dp[n][x]!=-1) return dp[n][x]; + long long int ans=0; + for(int i=1 ; i<=k ; i++){ + ans+=(solve(n-1, k, x-i, dp)%M); + } + return dp[n][x]=ans; + } + int numRollsToTarget(int n, int k, int target) { + vector> dp(n+1, vector(target+1, -1)); + long long int ans=solve(n,k,target,dp); + return ans%M; + } +};