题目如下:

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

解题思路:记dp[i][j] 为前i个骰子掷完后总和为j的组合的总数。那么很显然(i-1)个骰子掷完后的总和只能是  (j-d)  ~ (j-1) ,所以有dp[i][j] = sum(dp[i-1][j-d] , dp[i-1][j-d + 1] .... + dp[i-1][j-1]) 。

代码如下:

class Solution(object):
def numRollsToTarget(self, d, f, target):
"""
:type d: int
:type f: int
:type target: int
:rtype: int
"""
if target > d*f:
return 0
dp = [[0] * (d*f+1) for i in range(d)]
for i in range(1,f+1):
dp[0][i] = 1 for i in range(1,len(dp)):
for j in range(len(dp[i])):
for k in range(1,f+1):
dp[i][j] += dp[i-1][j-k]
#print dp return dp[-1][target] % (10**9 + 7)

【leetcode】1155. Number of Dice Rolls With Target Sum的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  2. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  5. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

  6. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  7. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

  8. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  9. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

随机推荐

  1. 快速入门分布式消息队列之 RabbitMQ(3)

    目录 目录 前文列表 前言 通道 Channel 一个基本的生产者消费者实现 消费者 生产者 运行结果 应用预取计数 应用 ACK 机制 最后 前文列表 快速入门分布式消息队列之 RabbitMQ(1 ...

  2. Delphi XE2 之 FireMonkey 入门(37) - 控件基础: TControl 概览

    Delphi XE2 之 FireMonkey 入门(37) - 控件基础: TControl 概览 { TControl } public   constructor Create(...); ov ...

  3. Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它

    Delphi XE2 之 FireMonkey 入门(35) - 控件基础: TFmxObject: 其它 TFmxObject 增加了 TagObject.TagFloat.TagString, 算 ...

  4. 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_3 Mybatis中编写dao实现类的使用-修改删除等其他操作

    update和上面的Insert代码基本是一样的,只需要修改这里, 测试Update的方法 删除 findById 测试方法 findByName 测试方法 findTotal

  5. Unity Audio Source Properties

    Audio Clip 音频剪辑 将播放声音的剪辑文件 Mute 静音 Bypass Effects 直通效果 应用音频源的快速“直通”过滤效果.一个简单的方法来打开/关闭所有特效 Output 产量  ...

  6. docker windows下挂载目录和文件

    我们利用docker启动项目的时候不能直接修改容器中的内容,只能在  run  的时候挂载到本地目录或者文件来进行修改. 例子:(路径可以忽略斜杠和反斜杠,我这边使用windows的路径没有报错.do ...

  7. 【ABAP系列】SAP ABAP 实现FTP的文件上传与下载

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 实现FTP的文 ...

  8. Go语言入门篇-高级数据类型

    一.数组类型 二.切片类型 切片的更多操作方法 示例: 三.字典类型 四.通道类型 示例: 通道的更多种类 示例: 五.函数 示例: 六.结构体和方法 示例: 七.接口 八.指针 示例: mooc

  9. python+selenium模拟键盘输入

    from selenium.webdriver.common.keys import Keys #键盘导入类 --------------------------------------------- ...

  10. python+selenium下弹窗alter对象处理02

    首先使用switch_to.alert()方法进行定位,然后可以使用下面的操作 text:返回alert.confirm.prompt中的文字信息: accept():接受现有警告框: dismiss ...