[InterviewBit] Dice Throw

Dice Throw

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
long long dp[1010][1010];
long long mod = 1e9 + 7;
long long helper(int d, int ma, int c) {
if(c < 0) return 0;
if(dp[d][c] != -1) return dp[d][c];
if(d <= 0) return 0;
long long& res = dp[d][c] = 0;
for(int i = 1; i <= min(ma,c - d + 1); i++) {
res = (res + helper(d-1,ma,c-i)) % mod;
}
return res;
}
int Solution::findDiceSum(int A, int B, int C) {
memset(dp, -1, sizeof dp);
dp[0][0] = 1;
return helper(A,B,C);
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/17/PS/interviewbit/dice-throw/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.