当前位置:网站首页>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];
}
}边栏推荐
- [medical segmentation] attention Unet
- Read PG in data warehouse in one article_ stat
- The difference and working principle between compiler and interpreter
- 水平垂直居中 方法 和兼容
- 模块六
- 【DesignMode】享元模式(Flyweight Pattern)
- Interface oriented programming
- 3000 words speak through HTTP cache
- 如何快速检查钢网开口面积比是否符合 IPC7525
- Three. JS series (1): API structure diagram-1
猜你喜欢
3000 words speak through HTTP cache

time标准库

Spark Tuning (III): persistence reduces secondary queries

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

Introduction and use of gateway

掌握这个提升路径,面试资料分享

HAVE FUN | “飞船计划”活动最新进展

Binary search tree (features)
As an Android Developer programmer, Android advanced interview

Master this promotion path and share interview materials
随机推荐
laravel构造函数和中间件执行顺序问题
Have fun | latest progress of "spacecraft program" activities
Module VI
Introduction and use of gateway
Sort out several important Android knowledge and advanced Android development interview questions
Build an all in one application development platform, light flow, and establish a code free industry benchmark
[medical segmentation] attention Unet
二叉搜索树(特性篇)
laravel post提交数据时显示异常
Personal notes of graphics (1)
Three. JS series (1): API structure diagram-1
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
AutoLISP series (3): function function 3
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
谈谈 SAP 系统的权限管控和事务记录功能的实现
spark调优(三):持久化减少二次查询
How can laravel get the public path
Laravel constructor and middleware execution order
LocalStorage和SessionStorage
整理几个重要的Android知识,高级Android开发面试题