当前位置:网站首页>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];
}
}边栏推荐
- Pisa-Proxy SQL 解析之 Lex & Yacc
- 全网“追杀”钟薛高
- logback. XML configure logs of different levels and set color output
- Prediction - Grey Prediction
- What is the difference between IP address and physical address
- AutoLISP series (2): function function 2
- The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
- 01tire+ chain forward star +dfs+ greedy exercise one
- Opportunity interview experience summary
- 记录Servlet学习时的一次乱码
猜你喜欢

最新Android面试合集,android视频提取音频

The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit

AutoLISP series (1): function function 1

Personal notes of graphics (4)

AutoLISP series (2): function function 2

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑

华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现

Arduino 控制的双足机器人

Sort out several important Android knowledge and advanced Android development interview questions

模块六
随机推荐
typescript ts 基础知识之类型声明
Binary search tree (features)
Pycharm terminal enables virtual environment
laravel构造函数和中间件执行顺序问题
LeetCode-SQL第一天
Personal notes of graphics (2)
Record the migration process of a project
Tragedy caused by deleting the console statement
面试题 01.02. 判定是否互为字符重排-辅助数组算法
掌握这套精编Android高级面试题解析,oppoAndroid面试题
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
模块六
C语言进阶——函数指针
Usage of config in laravel
最新高频Android面试题目分享,带你一起探究Android事件分发机制
运算符
Build an all in one application development platform, light flow, and establish a code free industry benchmark
php 自带过滤和转义函数
Have fun | latest progress of "spacecraft program" activities
Temperature sensor chip used in temperature detector