当前位置:网站首页>完全背包问题
完全背包问题
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];
}
边栏推荐
- PHP batch Excel to word
- Chapter 03_ User and authority management
- HTB-Lame
- 打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
- [applet project development -- JD mall] uni app commodity classification page (first)
- Lavaweb [first understanding the solution of subsequent problems]
- Redis分布式锁的8大坑
- Keil5中如何做到 0 Error(s), 0 Warning(s).
- 倍福TwinCAT3 Ads相关错误详细列表
- Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
猜你喜欢

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

监听器 Listener
![[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)](/img/73/a22ab1dbb46e743ffd5f78b40e66a2.png)
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)

Mysql知识点

So easy 将程序部署到服务器

数据交换 JSON

HTB-Lame

HTB-Lame

PHP batch Excel to word

How to verify whether the contents of two files are the same
随机推荐
[QT] add knowledge supplement of third-party database
力扣-两数之和
雪崩问题以及sentinel的使用
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS
Depth first traversal of C implementation Diagram -- non recursive code
Introduction and installation of Solr
Detailed list of errors related to twincat3 ads of Beifu
POI导出excel,按照父子节点进行分级显示
【EXSI】主机间传输文件
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
过滤器 Filter
访问url 404 的错误
线程数据共享和安全 -ThreadLocal
8 pits of redis distributed lock
E15 solution for cx5120 controlling Huichuan is620n servo error
Mysql知识点
Basic concept and classification of sorting
Overview of EtherCAT principle
How to verify whether the contents of two files are the same