Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Number of Dice Rolls With Target Sum

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1155" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 kn 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 109 + 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 Explanation: You throw two dice, each with 6 faces. There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1. Example 3: Input: n = 30, k = 30, target = 500 Output: 222616187 Explanation: The answer must be returned modulo 109 + 7. Constraints: 1 <= n, k <= 30 1 <= target <= 1000

Explanation

Here's an efficient solution to the dice roll problem:

  • Dynamic Programming: We use dynamic programming to store and reuse the number of ways to achieve a specific sum with a specific number of dice. The core idea is to build a table dp[i][j] representing the number of ways to get a sum j using i dice.

  • Bottom-Up Approach: The DP table is filled in a bottom-up manner. The base case is when we have only one die (n=1). We iterate through all possible target values and populate the first row of the DP table. Then, for subsequent dice, we iterate through all possible target values and consider all possible face values of the current die.

  • Modulo Arithmetic: To prevent integer overflow, we take the modulo 109 + 7 at each step.

  • Time and Space Complexity: O(n target k), O(n * target)

Code

    def numRollsToTarget(n: int, k: int, target: int) -> int:
    """
    Calculates the number of ways to roll n dice with k faces each to reach a target sum.

    Args:
        n: The number of dice.
        k: The number of faces on each die.
        target: The target sum.

    Returns:
        The number of ways to reach the target sum, modulo 10^9 + 7.
    """

    MOD = 10**9 + 7

    # dp[i][j] stores the number of ways to get sum j using i dice
    dp = [[0] * (target + 1) for _ in range(n + 1)]

    # Base case: if we have 0 dice, there is 1 way to get a sum of 0.
    dp[0][0] = 1

    # Iterate through the number of dice
    for i in range(1, n + 1):
        # Iterate through possible target sums
        for j in range(1, target + 1):
            # Iterate through possible values of the current die
            for face in range(1, min(j, k) + 1):
                dp[i][j] = (dp[i][j] + dp[i - 1][j - face]) % MOD

    return dp[n][target]

More from this blog

C

Chatmagic blog

2894 posts