forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
29 lines (29 loc) · 992 Bytes
/
Solution2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int dieSimulator(int n, int[] rollMax) {
int[][][] f = new int[n + 1][7][16];
for (int j = 1; j <= 6; ++j) {
f[1][j][1] = 1;
}
final int mod = (int) 1e9 + 7;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= 6; ++j) {
for (int x = 1; x <= rollMax[j - 1]; ++x) {
for (int k = 1; k <= 6; ++k) {
if (k != j) {
f[i][k][1] = (f[i][k][1] + f[i - 1][j][x]) % mod;
} else if (x + 1 <= rollMax[j - 1]) {
f[i][j][x + 1] = (f[i][j][x + 1] + f[i - 1][j][x]) % mod;
}
}
}
}
}
int ans = 0;
for (int j = 1; j <= 6; ++j) {
for (int x = 1; x <= rollMax[j - 1]; ++x) {
ans = (ans + f[n][j][x]) % mod;
}
}
return ans;
}
}