LeetCode 1155. Number of Dice Rolls With Target Sum

原题链接在这里:https://leetcode.com/problems/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

题解:

Let dp[k] denotes the way to make up target k.

For each dice, there could be value j within [1, f]. dp[k - j] is accumlated to new value.

New value need all the original values of dp, thus here we need a temp.

Time Complexity: O(d * f * target).

Space: O(target).

AC Java:

 1 class Solution {
 2     public int numRollsToTarget(int d, int f, int target) {
 3         int mod = 1000000007;
 4         int [] dp = new int[target + 1];
 5         dp[0] = 1;
 6
 7         for(int i = 0; i < d; i++){
 8             int [] temp = new int[target + 1];
 9             for(int j = 1; j <= f; j++){
10                 for(int k = j; k <= target; k++){
11                     temp[k] = (temp[k] + dp[k - j]) % mod;
12                 }
13             }
14
15             dp = temp;
16         }
17
18         return dp[target];
19     }
20 }

类似Coin Change 2.

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12196018.html

时间: 2024-10-12 03:07:42

LeetCode 1155. Number of Dice Rolls With Target Sum的相关文章

Leetcode-1155 Number of Dice Rolls With Target Sum(掷骰子的N种方法)

dp[i][j]表示前i个骰子到达数字总和j的方案数 dp[i][j] = Σdp[i-1][j-k],其中k是一个骰子能掷出的范围 1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 3 class Solution 4 { 5 public: 6 int dp[31][1003]; 7 int numRollsToTarget(int d, int f, int target) 8 { 9 _for(k,1,f+1) 10 dp[0][k]

[Leetcode] DP -- Target Sum

You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol. Find out how many ways to assign symbols to make sum of integers

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using