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
| class Solution { private static final int MOD = (int) 1e9 + 7; int[][] cache;
public int numRollsToTarget(int n, int k, int target) { if (target < n || target > n * k) return 0; cache = new int[n + 1][target - n + 1]; for (int[] c : cache) { Arrays.fill(c, -1); } return dfs(n, target - n, k); }
private int dfs(int i, int j, int k) { if (i == 0) { return j == 0 ? 1 : 0; } if (cache[i][j] != -1) { return cache[i][j]; } int ans = 0; for (int x = 0; x < k && x <= j; ++x) { ans = (ans + dfs(i - 1, j - x, k)) % MOD; } return cache[i][j] = ans; } }
|