2023-12-26 23:58:37 +01:00
|
|
|
class Solution {
|
2024-03-02 21:01:53 +01:00
|
|
|
private static final long MOD = 1000000007;
|
2023-12-26 23:58:37 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
public int numRollsToTarget(int n, int k, int target) {
|
|
|
|
// out of bounds
|
|
|
|
if (n > target || target > n * k) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-12-26 23:58:37 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
long[][] dp = new long[n + 1][target + 1];
|
|
|
|
dp[0][0] = 1;
|
2023-12-26 23:58:37 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
for (int toss = 1; toss <= n; ++toss) {
|
|
|
|
for (int sumIdx = toss, maxSumIdx = Math.min(target, toss * k);
|
|
|
|
sumIdx <= maxSumIdx;
|
|
|
|
++sumIdx) {
|
|
|
|
for (int f = 1; f <= Math.min(k, sumIdx); ++f) {
|
|
|
|
dp[toss][sumIdx] = (dp[toss][sumIdx] + dp[toss - 1][sumIdx - f]) % MOD;
|
2023-12-26 23:58:37 +01:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
}
|
2023-12-26 23:58:37 +01:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
|
|
|
|
return (int) dp[n][target];
|
|
|
|
}
|
2023-12-26 23:58:37 +01:00
|
|
|
}
|