mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
java: add “1155. Number of Dice Rolls With Target Sum”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d3bc57de18
commit
fdc2539ddb
1 changed files with 28 additions and 0 deletions
28
java/number-of-dice-rolls-with-target-sum.java
Normal file
28
java/number-of-dice-rolls-with-target-sum.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
class Solution {
|
||||
private static final long MOD = 1000000007;
|
||||
|
||||
public int numRollsToTarget(int n, int k, int target) {
|
||||
// out of bounds
|
||||
if (n > target || target > n * k) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
long[][] dp = new long[n + 1][target + 1];
|
||||
dp[0][0] = 1;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (int) dp[n][target];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue