当前位置:网站首页>LeetCode 1155. 擲骰子的N種方法**
LeetCode 1155. 擲骰子的N種方法**
2022-06-09 02:37:00 【暮雨林鐘】
具體思路:
完全背包問題,被卡的一塌胡塗;
看了三葉的才知道和傳統01寫的大差不差;
其實傳遞過去應該是一個階梯形,但是從0開始遍曆無傷大雅;
具體代碼:
class Solution {
public:
int numRollsToTarget(int n, int k, int target) {
int mod=1e9+7;
vector<vector<int>>dp(n+1,vector<int>(target+1,0));
dp[0][0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<=target;j++){
for(int u=1;u<=k;u++){
if(j>=u){
dp[i][j]=(dp[i][j])%mod+(dp[i-1][j-u])%mod;
dp[i][j]%=mod;
}
}
}
}
return dp[n][target];
}
};
class Solution {
public:
int numRollsToTarget(int n, int k, int target) {
if(target>k*n)
return 0;
int mod=1e9 + 7;
vector<vector<int>>dp(n+1,vector<int>(target+1,0));
dp[0][0]=1;
for(int i=0;i<=min(k,target);i++){
dp[1][i]=1;
}
for(int i=2;i<=n;i++){
for(int j=i;j<=target;j++){
for(int u=1;u<=k;u++){
if(j-u>0){
dp[i][j]=(dp[i][j])%mod+(dp[i-1][j-u])%mod;
dp[i][j]%=mod;
}
}
}
}
return dp[n][target];
}
};
边栏推荐
- 杰理之SPI 主机如何配置驱动程序?【篇】
- [MySQL from Xiaobai to expert] Part 6: transaction and JDBC programming in MySQL
- (10.3)【隐写缓解】隐写防护、隐写干扰、隐写检测
- 力扣解法汇总1037-有效的回旋镖
- 蓝桥杯_丢番图方程
- Navicat tool batch imports JSON format data to Doris
- 4426 divisible substring (enumeration + number theory)
- Exporter les connaissances pertinentes
- [network protocol] | [01] network byte order big end and small end
- Basic usage of flask sqlalmy
猜你喜欢

Two important influencing factors of togglerowselection() failure

How does the technical leader bring down a team?

Redis cluster setup
![[MySQL from Xiaobai to expert] Part 6: transaction and JDBC programming in MySQL](/img/e1/0807247eaf70d0fc95c3399352f1c8.png)
[MySQL from Xiaobai to expert] Part 6: transaction and JDBC programming in MySQL

Basic method of missing data filling (3) -- multiple imputation by chained equations (mice)

Docker installation redis

在业务代码中使用redis实现缓存效果

飞书要不要做生态?剖析第一家 All in 飞书的独立 SaaS 案例

Go技術日報(2022-06-07)——go程序員開發效率神器匯總

The 600 yuan Tiktok cloud bouncing Di live studio project is the source code of the tuyere project that rewards the income from live broadcasting
随机推荐
Tell me about the bug that impressed you
Gunicorn 20.0.4 request smuggling vulnerability
ClassNotFoundException vs NoClassDefFoundError
Jsnpp框架的全链式语法初探
FRP construction
Fight the high vision medical service of HKEx again, the gospel of short-sighted partners?
C# 类和对象
Blue Bridge Cup_ Frog date_ Extended Euclid
说说你印象中比较深刻的 Bug
Rcgi column - region of Overseas Social Market Research (including lottery)
Should flying books be ecological? Analyze the first independent SaaS case of all in flybook
NFT chain game system development | defi+nft technology construction
动态规划/斐波那契数列
SQLite3 syntax (2)
Jerry's SPI host [chapter]
贪心法/哈夫曼编码
Interface test series - interface test practice of transfer transaction business scenarios
How does Jerry's SPI slave configure the driver? [chapter]
What are the focuses of testing, pre release and production environment testing?
杰理之SPI主机【篇】