当前位置:网站首页>完全背包问题
完全背包问题
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];
}
边栏推荐
- [applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
- Basic concept and classification of sorting
- 最新接口自动化面试题
- gcc使用、Makefile总结
- 力扣-两数之和
- Introduction to webrtc concept -- an article on understanding source, track, sink and mediastream
- C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
- How do spark tasks of 10W workers run? (Distributed Computing)
- Stop saying that you can't solve the "cross domain" problem
- Subnet division (10)
猜你喜欢

E15 solution for cx5120 controlling Huichuan is620n servo error
![[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)

【机器学习】向量化计算 -- 机器学习路上必经路

JUC learning

第03章_用户与权限管理

Huawei operator level router configuration example | configuration static VPLS example
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

MySQL knowledge points
![[machine learning] vectorized computing -- a must on the way of machine learning](/img/3f/d672bb254f845ea705b3a0ca10ee19.png)
[machine learning] vectorized computing -- a must on the way of machine learning

最新接口自动化面试题
随机推荐
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Detailed list of errors related to twincat3 ads of Beifu
HTB-Lame
pytest-fixture
Ridge regression and lasso regression
Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
md5sum操作
Design of serial port receiving data scheme
限流组件设计实战
调试定位导航遇到的问题总结
Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example
How to determine the progress bar loaded in the loading interface when opening the game
串口接收数据方案设计
Nacos
JS日常开发小技巧(持续更新)
【日常训练】1175. 质数排列
Huawei operator level router configuration example | BGP VPLS configuration example
XXL job User Guide
Druid监控统计数据源
过滤器 Filter