Advertisement

Number of Dice Rolls With Target Sum - LeetCode 1155 Solution

Number of Dice Rolls With Target Sum - Complete Solution Guide

Number of Dice Rolls With Target Sum is LeetCode problem 1155, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have n dice, and each dice has k faces numbered from 1 to k . Given three integers n , k , and target , return the number of possible ways (out of the k n total ways) to roll the dice, so the sum of the face-up numbers equals target . Since the answer may be too large, return it modulo 10 9 + 7 . Example 1: Input: n = 1, k = 6, target = 3 Output: 1 Explanation: You throw one die with 6 faces. There is only one way to get a sum of 3. Example 2: Input: n = 2, k = 6, target = 7 Output: 6 Explan

Detailed Explanation

The problem asks us to find the number of ways to roll 'n' dice, each with 'k' faces (numbered 1 to k), such that the sum of the face-up numbers equals 'target'. Since the number of ways can be very large, we need to return the result modulo 10^9 + 7. For example, if we have 2 dice with 6 faces each, and the target sum is 7, then the possible combinations are (1, 6), (2, 5), (3, 4), (4, 3), (5, 2), and (6, 1), so the answer is 6.

Solution Approach

The provided solution uses dynamic programming with space optimization. It builds a table `dp` where `dp[i]` represents the number of ways to achieve a sum of `i` using a certain number of dice. The algorithm iterates through each die. For each die, it updates the `dp` array based on the possible outcomes of rolling that die. The space optimization is achieved by using a `new_dp` array that is updated in each iteration and then assigned to `dp`, effectively reusing the same space. The number of ways is accumulated by summing the number of ways to reach a sum 'j-1' from the previous dice and adding them to the current total. The 'rolling' feature ensures that results from the *previous* dice roll only are used, and sums greater than *k* are removed.

Step-by-Step Algorithm

  1. Step 1: Initialize a `dp` array of size `target + 1` with all elements set to 0. `dp[0]` is set to 1, representing the base case where a sum of 0 can be achieved with 0 dice (or before any dice are rolled).
  2. Step 2: Iterate `n` times, where `n` is the number of dice.
  3. Step 3: In each iteration, create a `new_dp` array of size `target + 1` and initialize all elements to 0.
  4. Step 4: Iterate from `j = 1` to `target`. For each `j`, calculate the number of ways to achieve the sum `j` by considering the possible values of the current die. This is done by summing the values of `dp[j-1]`. Accumulating `dp[j-1]` in `new_dp[j]`.
  5. Step 5: If `j > k` (where k is the number of faces on the die), it means we have considered sums that are impossible with the current die (since the minimum value is 1, sums can be greater than k). We remove the contribution from `dp[j - k - 1]` because `j - (k+1) + k >= j` implies that value can be reached. We do the modulus and addition to ensure no underflow errors.
  6. Step 6: After the inner loop completes, update `dp` to `new_dp` to prepare for the next iteration.
  7. Step 7: After `n` iterations, the value of `dp[target]` will contain the number of ways to achieve the target sum using `n` dice. Return `dp[target]`.

Key Insights

  • Insight 1: Dynamic programming is well-suited for this problem because the number of ways to reach a certain sum with a certain number of dice can be built upon the number of ways to reach smaller sums with fewer dice.
  • Insight 2: We can optimize space by using a rolling array technique to store only the results from the previous iteration, rather than a full 2D table.
  • Insight 3: The modulo operation is crucial to prevent integer overflow as the number of combinations can grow very quickly.

Complexity Analysis

Time Complexity: O(n*target)

Space Complexity: O(target)

Topics

This problem involves: Dynamic Programming.

Companies

Asked at: StackAdapt.