leetcode - 1155. Number of Dice Rolls With Target Sum

Description

You have n dice, and each die 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 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
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

Solution

Recursive + memorization

The dp transformation equation is:
d p [ n ] [ t a r g e t ] = ∑ i = 1 k d p [ n − 1 ] [ t a r g e t − i ] dp[n][target] = \sum_{i=1}^{k}dp[n-1][target-i] dp[n][target]=i=1∑kdp[n−1][target−i]

So we could do recursive and memorization or dp.

Time complexity: o ( n ∗ t a r g e t ∗ k ) o(n*target*k) o(n∗target∗k)

Space complexity: o ( n ∗ t a r g e t ) o(n*target) o(n∗target)

DP

Feels top-bottom (recursive + memo) is easier to implement than bottom-top (dp here)

Code

Recursive + memorization

python3 复制代码
class Solution:
    def numRollsToTarget(self, n: int, k: int, target: int) -> int:
        def helper(n: int, target: int) -> int:
            if (n, target) in memo:
                return memo[(n, target)]
            if n == 1:
                if 0 < target <= k:
                    memo[(n, target)] = 1
                else:
                    memo[(n, target)] = 0
                return memo[(n, target)]
            res = 0
            for i in range(1, k + 1):
                res += helper(n - 1, target - i)
                res %= mod_val
            memo[(n, target)] = res
            return memo[(n, target)]
        mod_val = 1000000007
        memo = {}
        return helper(n, target)

DP

python3 复制代码
class Solution:
    def numRollsToTarget(self, n: int, k: int, target: int) -> int:
        mod_val = 1000000007
        dp = [[0] * (target + 1) for _ in range(n + 1)]
        # init
        for pseudo_target in range(1, min(target + 1, k + 1)):
            dp[1][pseudo_target] = 1
        for pseudo_n in range(2, n + 1):
            for pseudo_t in range(1, target + 1):
                for i in range(1, k + 1):
                    if pseudo_t - i >= 0:
                        dp[pseudo_n][pseudo_t] += dp[pseudo_n - 1][pseudo_t - i]
                dp[pseudo_n][pseudo_t] %= mod_val
        return dp[-1][-1]
相关推荐
快手技术几秒前
快手提出端到端生成式搜索框架 OneSearch,让搜索“一步到位”!
算法
CoovallyAIHub20 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP21 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo21 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo21 小时前
300:最长递增子序列
算法
CoovallyAIHub1 天前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI2 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v2 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工2 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法