当前位置:网站首页>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];
}
}
关于背包问题,参考文章.
边栏推荐
猜你喜欢

2.8K 120Hz触控双屏加持 灵耀X 双屏Pro 2022让办公无惧想象

软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?

阿里官方 Redis 开发规范

今晚直播!

MySQL最大建议行数2000w, 靠谱吗?

南京科技大学、中国电子科技第28研究所等联合|MLRIP: Pre-training a military language representation model with informative factual knowledge and professional knowledge base(预训练具有丰富事实知识和专业知识库的军事语言表示模型)

我的新书销量1万册了!

Shell basic function writing

2022年7月最热的10篇AI论文

MLX90640 红外热成像仪测温模块开发笔记(完整版)
随机推荐
MySQL【创建和管理表】
“查找附近的商铺”|Geohash+MySQL实现地理位置筛选
Synchronized原理
How to Efficiently Develop Jmix Extension Components
选择合适的 DevOps 工具,从理解 DevOps 开始
MySQL可以做多台vps的双向同步吗?
Vulnhub靶机:HARRYPOTTER_ NAGINI
行程排序(暑假每日一题 12)
1个月写900多条用例,2线城市年薪33W+的测试经理能有多卷?
【Unity,C#】哨兵点位循迹模板代码
打破文件锁限制,以存储力量助力企业增长新动力
Eslint syntax error is solved
【Untitled】
kubelet node pressure eviction
第一次改开源中间件keycloak总个结
C#的DataTable帮助类
Use Canvas to implement mobile phone signature
首席工程师究竟是怎样的存在?
比对软件-blastN结果详解
等变图神经网络在药物研发中大放异彩