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 dpntarget = \sum_{i=1}^{k}dpn-1target-i dpntarget=i=1∑kdpn−1target−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]
相关推荐
云泽8088 小时前
笔试算法 - 链表篇(一):移除、反转、合并、回文判断全解析
数据结构·c++·算法·链表
菜菜的顾清寒8 小时前
HOT力扣100(43)二叉树-翻转二叉树
数据结构·算法·leetcode
通信小呆呆9 小时前
Toeplitz结构及其快速算法详解
算法
YikNjy9 小时前
break和continue
java·开发语言·算法
春日见9 小时前
五分钟入门 强化学习---DQN(Deep Q Net)算法与实现
人工智能·python·深度学习·算法·microsoft·机器学习
秦明月139 小时前
水冷板装配安全回路设计实战解析
经验分享·其他·职场和发展·创业创新·学习方法
budingxiaomoli9 小时前
动态规划--斐波那契数列模型
算法·动态规划
IT猿手9 小时前
多目标优化算法:多目标蛇优化算法(Multiple Objective Snake Optimizer,MOSO)(提供MATLAB代码)
开发语言·算法·matlab·动态路径规划·光伏模型参数估计
MegaDataFlowers9 小时前
101.对称二叉树
算法
Jasmine_llq10 小时前
《B3939 [GESP样题 四级] 绝对素数》
数据结构·算法·素数判断算法·数字拆分与反转算法·区间遍历枚举·双条件判断逻辑