当前位置:网站首页>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];
}
}
边栏推荐
- laravel post提交数据时显示异常
- [designmode] proxy pattern
- [designmode] facade patterns
- 爬虫(17) - 面试(2) | 爬虫面试题库
- time标准库
- 23. 合并K个升序链表-c语言
- [medical segmentation] attention Unet
- Talk about the realization of authority control and transaction record function of SAP system
- Record the migration process of a project
- Lowcode: four ways to help transportation companies enhance supply chain management
猜你喜欢
随机推荐
typescript ts基础知识之tsconfig.json配置选项
Lowcode: four ways to help transportation companies enhance supply chain management
二叉搜索树(特性篇)
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
应用在温度检测仪中的温度传感芯片
Opencv configuration 2019vs
Master this promotion path and share interview materials
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
Find tags in prefab in unity editing mode
laravel构造函数和中间件执行顺序问题
Sqlserver2014+: create indexes while creating tables
射线与OBB相交检测
值得一看,面试考点与面试技巧
面向接口编程
记一次项目的迁移过程
Cesium(3):ThirdParty/zip. js
Horizontal and vertical centering method and compatibility
Binary search tree (features)
os、sys、random标准库主要功能
[C language] question set of X