当前位置:网站首页>Complete knapsack problem to find the number of combinations and permutations
Complete knapsack problem to find the number of combinations and permutations
2022-08-01 16:32:00 【North Sea ghost fish not sleeping】
This is a typical application of the complete knapsack problem,Because it's just asking for numbers,It does not involve the case where the arrangement of the change is different and counted twice,所以两层for循环,外层遍历物品,内层遍历背包.
class Solution {
public int change(int amount, int[] coins) {
int[] dp = new int[amount+1];
dp[0] = 1;
for (int coin : coins) {
for (int i = coin;i<= amount;i++){
dp[i] += dp[i-coin];
}
}
return dp[amount];
}
}
The only difference between this one and the one above is the ordering,Different order counts twice,So here are two layersforThe traversal order of the loop is 外层遍历背包,内层遍历物品.
class Solution {
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for(int j = 1;j<=target;j++){
for(int i = 0;i<nums.length;i++){
if(j>=nums[i]) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}
}
关于背包问题,参考文章.
边栏推荐
- 【硬核拆解】50块2个的2022年夏季款智能节电器到底能不能省电?
- 软测面试如何介绍项目?要做哪些技术准备?
- moxa串口服务器配置说明(moxa串口驱动)
- 2022年7月最热的10篇AI论文
- 探讨if...else的替代方案
- 每日优鲜大败局
- 【黑马早报】胡军代言悟空理财涉嫌欺诈,疑似涉及390亿元;涪陵榨菜回应榨菜吃出脚指甲; 鸿星尔克再捐1个亿;腾讯控股股价跌回5年前...
- “查找附近的商铺”|Geohash+MySQL实现地理位置筛选
- 行程排序(暑假每日一题 12)
- Zhaoqi Science and Technology Innovation Event Platform, Entrepreneurship Event Roadshow, Online Live Roadshow
猜你喜欢
AntDB数据库亮相24届高速展,助力智慧高速创新应用
22年镜头“卷”史,智能手机之战卷进死胡同
工业制造行业的低代码开发平台思维架构图
OneFlow源码解析:Op、Kernel与解释器
ESP8266-Arduino programming example-GA1A12S202 logarithmic scale analog light sensor
js邯郸市地图网页源码下载
MySQL INTERVAL 关键字指南
pynlpir更新license Error: unable to fetch newest license解决方案
软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?
A full review of mainstream timed task solutions
随机推荐
【Unity,C#】哨兵射线触发器模板代码
【Untitled】
05 doris 集群搭建
AI艺术‘美丑’不可控?试试 AI 美学评分器~
Flink - SQL can separate a certain parallelism of operator node configuration?
软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?
1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?
直播app开发,是优化直播体验不得不关注的两大指标
等变图神经网络在药物研发中大放异彩
Go 单元测试
MySQL可以做多台vps的双向同步吗?
测试技术|白盒测试以及代码覆盖率实践
Using Canvas to achieve web page mouse signature effect
月薪12K,蝶变向新勇往直前,我通过转行软件测试实现月薪翻倍...
Vulnhub靶机:HARRYPOTTER_ NAGINI
ESP8266-Arduino编程实例-GA1A12S202对数刻度模拟光传感器
请问nacos 连接mysql失败后会重新连接吗?
PAT 甲级 A1003 Emergency
使用Canvas 实现手机端签名
bug- 切换代理服务器与同步 bug