题目如下:

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. lambda表达式使用解析

    1.Predicate/Consumer/Function/Supplier介绍 Predicate boolean test(T t); Consumer accpet(T t); Function ...

  2. Selenium学习之==>WebDriverApi接口详解

    浏览器操作 driver.back() # 后退 driver.forward() # 前进 driver.refresh() # 刷新 窗口操作 driver.get_window_size() # ...

  3. springMVC+Spring+Mybatis+Redis

    SPRINGMVC+MYBATIS+SPRING+REDIS 只作参考,以防忘记使用! mybatis的配置文件: <?xml version="1.0" encoding= ...

  4. 2019暑假第二周(hadoop在个人电脑上的搭建)

    一,Hadoop和NoSQL数据库的学习,大多需要Linux环境. 搭建Linux环境可以分为两种方式: (1)在电脑上安装双操作系统,即同时安装Linux和Windows操作系统,在电脑启动的时候, ...

  5. vue引入query

    1 首先先安装query.(cnpm install query --save-dev)或者 在package.json里的dependencies加入"jquery" : &qu ...

  6. java程序启动脚本

    #!/bin/bash appName=`ls|grep .jar$` if [ -z $appName ] then echo "Please check that this script ...

  7. Android View的Adapter

    1 Adapter适配的对象是View Adapter通过为View提供指定格式的数据来适配View,让View可以以事先约定好的方式将内容展示给用户. 所以,进行UI设计的关键是搞清楚各个View组 ...

  8. 【监控笔记】【1.5】事件通知(event Notification)

    关键词:DDL监控 [监控笔记][1.5]事件通知(event Notification) 注意,只能通过删除新建来修改事件. [1]概念 事件通知是特殊类型的数据库对象,用于将有关服务器和数据库实践 ...

  9. Linux 最常用命令整理,建议收藏!

    Linux是目前应用最广泛的服务器操作系统,基于Unix,开源免费,由于系统的稳定性和安全性,市场占有率很高,几乎成为程序代码运行的最佳系统环境. linux不仅可以长时间的运行我们编写的程序代码,还 ...

  10. openmvg中cmd模块解析

    ---恢复内容开始--- 在openmvg库中,定义了一个CmdLine类来定义例程的输入参数格式.源文件为.\openMVG\src\third_party\cmdLine\cmdLine.h. 先 ...