[LeetCode] 1155. Number of Dice Rolls With Target Sum

You have d dice, and each die has f faces numbered 1, 2, ..., f.

Return the number of possible ways (out of fd total ways) modulo 10^9 + 7 to roll the dice so the sum of the face up numbers equals target.

Example 1:

Input: d = 1, f = 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: d = 2, f = 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: d = 2, f = 5, target = 10
Output: 1
Explanation: 
You throw two dice, each with 5 faces.  There is only one way to get a sum of 10: 5+5.

Example 4:

Input: d = 1, f = 2, target = 3
Output: 0
Explanation: 
You throw one die with 2 faces.  There is no way to get a sum of 3.

Example 5:

Input: d = 30, f = 30, target = 500
Output: 222616187
Explanation: 
The answer must be returned modulo 10^9 + 7.

Constraints:

  • 1 <= d, f <= 30
  • 1 <= target <= 1000

掷骰子的N种方法。

这里有 d 个一样的骰子,每个骰子上都有 f 个面,分别标号为 1, 2, ..., f。

我们约定:掷骰子的得到总点数为各骰子面朝上的数字的总和。

如果需要掷出的总点数为 target,请你计算出有多少种不同的组合情况(所有的组合情况总共有 f^d 种),模 10^9 + 7 后返回。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-dice-rolls-with-target-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意不难理解,就是给你 d 个骰子,每个骰子有 f 个面,让你求的是有多少种可能掷出的总点数为 target。一般这种问你有多少种可能性的,不是 backtracking 就是动态规划。但是介于题目说了结果需要取模,所以其实是提示你 input 数据有可能会很大,backtracking 做很有可能会超时。这里我提供两种思路,一是DFS + memorization,一种是 DP动态规划。

DFS + memorization 的思路如下,既然是记忆化递归,那么我们还是需要用一个hashmap来实现记忆的功能。这里hashmap的key是一个字符串,记录的是剩下的骰子个数 + target的值。递归函数的base case有如下几种

当剩下的骰子个数 * 骰子能投出的最大值 < target的时候,或者骰子个数 > target的时候,就可以退出了(这个条件是帮助剪枝)

当剩下的骰子个数 == 0 && target == 0,说明得到一个可行解了,返回1

当剩下的骰子个数 == 0 || target == 0,说明不符合条件,要不是骰子用完了,要不就是骰子还没用完target就是0了,返回0

时间O(d * f) - d个骰子,每个骰子有 f 个面

空间O(n) - hashmap

Java实现

 1 class Solution {
 2     int MOD = (int) Math.pow(10, 9) + 7;
 3     HashMap<String, Integer> memo = new HashMap<>();
 4 
 5     public int numRollsToTarget(int d, int f, int target) {
 6         // base case
 7         if (d * f < target || d > target) {
 8             return 0;
 9         }
10         if (d == 0 && target == 0) {
11             return 1;
12         }
13 
14         String str = d + " " + target;
15         if (memo.containsKey(str)) {
16             return memo.get(str);
17         }
18 
19         int res = 0;
20         for (int i = 1; i <= f; i++) {
21             if (target >= i) {
22                 res = (res + numRollsToTarget(d - 1, f, target - i)) % MOD;
23             } else {
24                 break;
25             }
26         }
27         memo.put(str, res);
28         return res;
29     }
30 }

 

动态规划的思路也是类似于DFS + memorization 的思路,同时这道题是属于动态规划里面的背包问题一类。这里我们需要一个二维矩阵 dp[i][j] 记录 DP 的值。第一维表示骰子的个数,第二位表示 target。DP 的定义是当掷了 i 个骰子后的值是多少。dp[i][j] 应该是从 dp[i - 1][j] 而来,如果当前这一次掷骰子的面值是K的话,那么上一次掷骰子的面值应该是 j - k,则有了方程 dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j - 2] + ... + dp[i - 1][j - f]

理解了这个 DP 的定义,代码就不难理解了。

时间O(d * f * k) - 骰子的个数 * f个面 * 前d - 1个骰子骰出来的值

空间O(mn) - m 个骰子,需要求前 n 个数字的 DP 值,直到 target

Java实现

 1 class Solution {
 2     int MOD = (int) Math.pow(10, 9) + 7;
 3 
 4     public int numRollsToTarget(int d, int f, int target) {
 5         int[][] dp = new int[31][1001];
 6         int min = Math.min(f, target);
 7         for (int i = 1; i <= min; i++) {
 8             dp[1][i] = 1;
 9         }
10         int targetMax = d * f;
11         for (int i = 2; i <= d; i++) {
12             for (int j = i; j <= targetMax; j++) {
13                 for (int k = 1; j - k >= 0 && k <= f; k++) {
14                     dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % MOD;
15                 }
16             }
17         }
18         return dp[d][target];
19     }
20 }

 

LeetCode 题目总结

posted @ 2021-04-05 13:48  CNoodle  阅读(176)  评论(0编辑  收藏  举报