当前位置:网站首页>LeetCode 1155. 掷骰子的N种方法 每日一题
LeetCode 1155. 掷骰子的N种方法 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
这里有 n 个一样的骰子,每个骰子上都有 k 个面,分别标号为 1 到 k 。
给定三个整数 n , k 和 target ,返回可能的方式(从总共 kn 种方式中)滚动骰子的数量,使正面朝上的数字之和等于 target 。
答案可能很大,你需要对 109 + 7 取模 。
示例 1:
输入:n = 1, k = 6, target = 3
输出:1
解释:你扔一个有6张脸的骰子。
得到3的和只有一种方法。
示例 2:输入:n = 2, k = 6, target = 7
输出:6
解释:你扔两个骰子,每个骰子有6个面。
得到7的和有6种方法1+6 2+5 3+4 4+3 5+2 6+1。
示例 3:输入:n = 30, k = 30, target = 500
输出:222616187
解释:返回的结果必须是对 109 + 7 取模。
提示:
1 <= n, k <= 30
1 <= target <= 1000来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int numRollsToTarget(int n, int k, int target) {
int[][] dp = new int[n + 1][target + 1];
int mod = (int)Math.pow(10,9) + 7;
dp[0][0] = 1;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= target;j++){
for(int q = 1;q <= k;q++){
if(j >= q)
dp[i][j] = (dp[i][j] + dp[i - 1][j - q]) % mod;
}
}
}
return dp[n][target];
}
}边栏推荐
- Find tags in prefab in unity editing mode
- Prediction - Grey Prediction
- Module VI
- laravel post提交数据时显示异常
- [Android -- data storage] use SQLite to store data
- Sort out several important Android knowledge and advanced Android development interview questions
- 水平垂直居中 方法 和兼容
- 深度监听 数组深度监听 watch
- Prometheus API deletes all data of a specified job
- 目标跟踪常见训练数据集格式
猜你喜欢

A tour of gRPC:03 - proto序列化/反序列化

全网“追杀”钟薛高

值得一看,面试考点与面试技巧

AutoLISP series (1): function function 1

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions

Personal notes of graphics (1)

time标准库

Introduction and use of gateway

Vs2019 configuration matrix library eigen

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
随机推荐
[medical segmentation] attention Unet
Cesium(3):ThirdParty/zip. js
Laravel5.1 Routing - routing packets
JS 模块化
logback.xml配置不同级别日志,设置彩色输出
Personal notes of graphics (4)
Advanced C language -- function pointer
Imitate the choice of enterprise wechat conference room
二叉搜索树(特性篇)
正在准备面试,分享面经
URL和URI的关系
【医学分割】attention-unet
Arduino 控制的双足机器人
最新阿里P7技术体系,妈妈再也不用担心我找工作了
LocalStorage和SessionStorage
Lie cow count (spring daily question 53)
应用在温度检测仪中的温度传感芯片
[designmode] flyweight pattern
Personal notes of graphics (2)
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑