Number of Dice Rolls With Target Sum (LeetCode 1155): Solution and Explanation
Count the ways n dice with faces 1..k can add up to target, where the dice are told apart (1+6 and 6+1 are different). Return the count modulo 10^9 + 7.
Intuition
Rolling all n dice at once is hard to count, but adding them one at a time is easy. Before the last die is rolled, the others have some total. If the last die shows f, that total must have been target - f.
So the number of ways to reach a sum s with i dice is the sum, over every face f, of the ways to reach s - f with i - 1 dice. Each face leads to a different roll, so nothing is counted twice.
Laid out as a table with one row per die and one column per sum, each cell is the total of a k-wide strip of cells in the row above, just to its left. Row 0 has a single 1 at sum 0: no dice, total zero, one way.
Approach: Row-by-row DP over dice
- Start with
dp[0] = 1and every other sum 0: zero dice can only make 0. - For each die
ifrom 1 ton, build a fresh arraynxtof lengthtarget + 1. - For each sum
sfrom 1 totarget, setnxt[s]to the sum ofdp[s - f]forf = 1..min(k, s), modulo10^9 + 7. - Replace
dpwithnxt. - After all dice, return
dp[target].
Step-by-step example
n = 2, k = 6, target = 7
- Row 0: dp = [1,0,0,0,0,0,0,0].
- Die 1: each sum 1..6 picks up the single 1 at sum 0, so row 1 is [0,1,1,1,1,1,1,0]. Sum 7 needs a face of 7, which does not exist.
- Die 2, sum 7: faces 1..6 point back to sums 6,5,4,3,2,1 of row 1, which are all 1.
- Adding them gives 6.
- The answer is 6: the pairs (1,6), (2,5), (3,4), (4,3), (5,2), (6,1).
Code: Python, Java, C++
1class Solution:
2 def numRollsToTarget(self, n: int, k: int, target: int) -> int:
3 MOD = 10**9 + 7
4 dp = [1] + [0] * target
5 for i in range(1, n + 1):
6 nxt = [0] * (target + 1)
7 for s in range(1, target + 1):
8 for f in range(1, min(k, s) + 1):
9 nxt[s] = (nxt[s] + dp[s - f]) % MOD
10 dp = nxt
11 return dp[target]1class Solution {
2 public int numRollsToTarget(int n, int k, int target) {
3 final int MOD = 1_000_000_007;
4 int[] dp = new int[target + 1];
5 dp[0] = 1;
6 for (int i = 1; i <= n; i++) {
7 int[] nxt = new int[target + 1];
8 for (int s = 1; s <= target; s++) {
9 for (int f = 1; f <= Math.min(k, s); f++) {
10 nxt[s] = (nxt[s] + dp[s - f]) % MOD;
11 }
12 }
13 dp = nxt;
14 }
15 return dp[target];
16 }
17}1class Solution {
2public:
3 int numRollsToTarget(int n, int k, int target) {
4 const int MOD = 1000000007;
5 vector<int> dp(target + 1, 0);
6 dp[0] = 1;
7 for (int i = 1; i <= n; i++) {
8 vector<int> nxt(target + 1, 0);
9 for (int s = 1; s <= target; s++) {
10 for (int f = 1; f <= min(k, s); f++) {
11 nxt[s] = (nxt[s] + dp[s - f]) % MOD;
12 }
13 }
14 dp = nxt;
15 }
16 return dp[target];
17 }
18};Time and space complexity
O(n * target * k)
Each of the n rows has target cells, and each cell adds up at most k values. With n, k <= 30 and target <= 1000 that is under a million additions.
O(target)
Only the previous row and the row being built are kept.
Other approaches
Sliding window sum (time O(n * target), space O(target))
Each cell adds a k-wide strip of the row above, and the strip for s + 1 is the strip for s shifted by one. Keep a running window sum to fill each row in O(target).
Top-down memoization (time O(n * target * k), space O(n * target))
Recurse on (dice left, sum left), trying every face, and cache the results. Same recurrence, often easier to write first.
Common mistakes
- Forgetting the modulo on each addition. The counts grow like k^n and overflow 32-bit integers long before the end.
- Updating one array in place from left to right. The cells you read must come from the previous die, so build a new row (or loop sums downward).
- Treating the dice as identical. Order matters here, which is exactly what summing over the last die's face counts.
- Letting s - f go negative. Limit f to min(k, s).
Frequently asked questions
Why is dp[0][0] equal to 1?
There is exactly one way to roll zero dice: do nothing, which totals 0. Every real roll is built on top of that empty start, so it must count once.
Can the answer ever be 0?
Yes. If target is below n (each die shows at least 1) or above n * k (each die shows at most k), no roll can reach it.
How is this related to Coin Change II?
Both add up values to reach a target. Coin Change II counts combinations where order does not matter and coins can repeat freely, while here there is exactly one value per die and order matters, so the outer loop runs over dice.