当前位置:网站首页>完全背包问题
完全背包问题
2022-07-01 03:13:00 【热心市民薛先生】
完全背包问题和0-1背包问题的差别为:完全背包物品的数量没有限制,而0-1背包每个物品的数量为1个。
//先遍历物品,再遍历背包
private static void testCompletePack(){
int[] weight = {
1, 3, 4};
int[] value = {
15, 20, 30};
int bagWeight = 4;
int[] dp = new int[bagWeight + 1];
for (int i = 0; i < weight.length; i++){
// 遍历物品
for (int j = weight[i]; j <= bagWeight; j++){
// 遍历背包容量
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
}
}
for (int maxValue : dp){
System.out.println(maxValue + " ");
}
}
//先遍历背包,再遍历物品
private static void testCompletePackAnotherWay(){
int[] weight = {
1, 3, 4};
int[] value = {
15, 20, 30};
int bagWeight = 4;
int[] dp = new int[bagWeight + 1];
for (int i = 1; i <= bagWeight; i++){
// 遍历背包容量
for (int j = 0; j < weight.length; j++){
// 遍历物品
if (i - weight[j] >= 0){
dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]);
}
}
}
for (int maxValue : dp){
System.out.println(maxValue + " ");
}
}
leetcode上的变形题,不是这种纯完全背包的问题了。
分为两种:
1、组合问题,组合问题不讲究先后顺序,{1,5}和{5,1}是一样的
2、排列问题,排列问题要注意先后顺序,{1,5}和{5,1}是两个答案
如果求组合数就是外层for循环遍历物品,内层for遍历背包。
如果求排列数就是外层for遍历背包,内层for循环遍历物品。

典型的组合问题,不要求顺序,{1,2,2} 和{2,1,2}是一样的,组成总金额为5的数,所需一枚一块和两枚两块的,不要求顺序,所以先遍历遍历物品,再遍历背包
public int change(int amount, int[] coins) {
int dp[] = new int[amount+1];
dp[0] = 1;
for(int i = 0;i <coins.length;i++){
for(int j = coins[i]; j <= amount; j++){
dp[j] += dp[j-coins[i]];
}
}
return dp[amount];

这道题求排列数,从测试用例可看出,所以先遍历背包,再遍历物品
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
//先遍历背包,再遍历物品
for(int i = 0;i <= target;i++){
for(int j = 0;j < nums.length;j++){
if(i >= nums[j]) dp[i] += dp[i-nums[j]];
}
}
return dp[target];
}
边栏推荐
猜你喜欢

Basic concepts of database
![[applet project development -- JD mall] uni app commodity classification page (Part 2)](/img/f3/752f41f5b5cc16c8a71498ea9cabb5.png)
[applet project development -- JD mall] uni app commodity classification page (Part 2)

Redis efficient like and cancel function

数据交换 JSON

Druid monitoring statistics source

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)

【小程序项目开发-- 京东商城】uni-app之分类导航区域

线程数据共享和安全 -ThreadLocal

# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'

Analyze datahub, a new generation metadata platform of 4.7K star
随机推荐
JS to find duplicate elements in two arrays
Servlet [first introduction]
Multithreaded printing
别再说不会解决 “跨域“ 问题啦
VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
Chapitre 03 Bar _ Gestion des utilisateurs et des droits
[linear DP] longest common subsequence
线程数据共享和安全 -ThreadLocal
Ctfshow blasting WP
一文讲解发布者订阅者模式与观察者模式
Redis分布式锁的8大坑
[applet project development -- Jingdong Mall] classified navigation area of uni app
Feign远程调用和Getaway网关
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
HTB-Lame
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
Huawei operator level router configuration example | configuration static VPLS example
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
Kmeans