当前位置:网站首页>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];
}
}
关于背包问题,参考文章.
边栏推荐
猜你喜欢
随机推荐
kubelet node pressure eviction
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!
蚂蚁首次披露核心基础软件技术开源版图
MUI as a mobile phone to return to the action bar
中国驻西班牙使馆再次提醒留学人员注意暑期安全
AntDB数据库亮相24届高速展,助力智慧高速创新应用
lombok builder重写
ECCV 2022 | Poseur:你以为我是姿态估计,其实是目标检测哒
90后的焦虑,被菜市场治好了
SQL函数 TIMESTAMPDIFF
C#中关于DevExpress的常用操作和帮助类项目工程内容说明
intentservice使用(Intention)
DOM树jsjs特效代码
如何有效地开发 Jmix 扩展组件
AI艺术‘美丑’不可控?试试 AI 美学评分器~
五分钟带你上手ShardingJDBC实现MySQL分库分表
How to Efficiently Develop Jmix Extension Components
月薪12K,蝶变向新勇往直前,我通过转行软件测试实现月薪翻倍...
HashCode technology insider interview must ask
nodejs安装淘宝镜像(配置淘宝镜像)









