当前位置:网站首页>LeetCode 1155. N ways to roll dice one question per day
LeetCode 1155. N ways to roll dice one question per day
2022-07-07 16:59:00 【@Little safflower】
Problem description
Here you are n The same dice , Every dice has k Face to face , They are labeled as 1 To k .
Given three integers n , k and target , Return to possible ways ( From a total of kn In different ways ) The number of rolling dice , Make the sum of the numbers facing up equal to target .
The answer may be very big , You need to 109 + 7 modulus .
Example 1:
Input :n = 1, k = 6, target = 3
Output :1
explain : You throw one with 6 Face dice .
obtain 3 And there is only one way .
Example 2:Input :n = 2, k = 6, target = 7
Output :6
explain : You throw two dice , Each die has 6 Face to face .
obtain 7 And there are 6 Methods 1+6 2+5 3+4 4+3 5+2 6+1.
Example 3:Input :n = 30, k = 30, target = 500
Output :222616187
explain : The returned result must be right 109 + 7 modulus .
Tips :
1 <= n, k <= 30
1 <= target <= 1000source : Power button (LeetCode)
link :https://leetcode.cn/problems/number-of-dice-rolls-with-target-sum
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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];
}
}
边栏推荐
猜你喜欢
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
【医学分割】attention-unet
Pycharm terminal enables virtual environment
作为Android开发程序员,android高级面试
Interface oriented programming
最新Android高级面试题汇总,Android面试题及答案
最新2022年Android大厂面试经验,安卓View+Handler+Binder
AutoLISP series (3): function function 3
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
【图像传感器】相关双采样CDS
随机推荐
LeetCode 1049. 最后一块石头的重量 II 每日一题
Personal notes of graphics (4)
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Three. JS series (1): API structure diagram-1
LeetCode 1155. 掷骰子的N种方法 每日一题
Direct dry goods, 100% praise
typescript ts 基础知识之类型声明
Lowcode: four ways to help transportation companies enhance supply chain management
Inner monologue of accidental promotion
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Opportunity interview experience summary
Personal notes of graphics (2)
Laravel5.1 Routing - routing packets
Seaborn数据可视化
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
AutoLISP series (3): function function 3
LeetCode 403. 青蛙过河 每日一题
字节跳动Android面试,知识点总结+面试题解析
掌握这个提升路径,面试资料分享