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]
相关推荐
林开落L10 分钟前
前缀和算法习题篇(上)
c++·算法·leetcode
远望清一色11 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
tyler_download13 分钟前
手撸 chatgpt 大模型:简述 LLM 的架构,算法和训练流程
算法·chatgpt
SoraLuna33 分钟前
「Mac玩转仓颉内测版7」入门篇7 - Cangjie控制结构(下)
算法·macos·动态规划·cangjie
我狠狠地刷刷刷刷刷36 分钟前
中文分词模拟器
开发语言·python·算法
鸽鸽程序猿37 分钟前
【算法】【优选算法】前缀和(上)
java·算法·前缀和
九圣残炎43 分钟前
【从零开始的LeetCode-算法】2559. 统计范围内的元音字符串数
java·算法·leetcode
YSRM1 小时前
Experimental Analysis of Dedicated GPU in Virtual Framework using vGPU 论文分析
算法·gpu算力·vgpu·pci直通
韭菜盖饭1 小时前
LeetCode每日一题3261---统计满足 K 约束的子字符串数量 II
数据结构·算法·leetcode
xxxmmc1 小时前
Leetcode 75 Sort colors
leetcode·三指针移动问题