当前位置:网站首页>Complete knapsack problem
Complete knapsack problem
2022-07-01 03:36:00 【Enthusiastic citizen Mr. Xue】
Complete knapsack problem and 0-1 The difference between knapsack problems is : There is no limit to the number of complete backpack items , and 0-1 The quantity of each item in the backpack is 1 individual .
// Go through the items first , And then traverse the backpack
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++){
// Traverse the items
for (int j = weight[i]; j <= bagWeight; j++){
// Traverse the backpack capacity
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
}
}
for (int maxValue : dp){
System.out.println(maxValue + " ");
}
}
// Go through the backpack first , Then traverse the items
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++){
// Traverse the backpack capacity
for (int j = 0; j < weight.length; j++){
// Traverse the items
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 Deformation problem on , It is not the problem of this kind of pure and complete backpack .
Divided into two :
1、 Combination question , Combinatorial problems do not pay attention to order ,{1,5} and {5,1} It's the same
2、 The problem of permutation , You should pay attention to the order of arrangement ,{1,5} and {5,1} There are two answers
If you find the combination number, it's the outer layer for Loop through items , Inner layer for Traverse the backpack .
If you find the permutation number, it's the outer layer for Traverse the backpack , Inner layer for Loop through items .

Typical combinatorial problems , Order is not required ,{1,2,2} and {2,1,2} It's the same , The total amount of the composition is 5 Number of numbers , One piece and two pieces are required , Order is not required , So go through the items first , And then traverse the backpack
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];

This problem is to find the number of permutations , It can be seen from the test cases , So go through the backpack first , Then traverse the items
public int combinationSum4(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
// Go through the backpack first , Then traverse the items
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];
}
边栏推荐
- Feature Pyramid Networks for Object Detection论文理解
- [deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
- 服务器渲染技术jsp
- 【伸手党福利】开发人员重装系统顺序
- Cookie&Session
- 5. [WebGIS practice] software operation - service release and permission management
- Leetcode: offer 59 - I. maximum value of sliding window
- File upload and download
- Filter
- Basic concepts of database
猜你喜欢
![5. [WebGIS practice] software operation - service release and permission management](/img/5d/070e207bd96e60ba1846d644d4fb54.png)
5. [WebGIS practice] software operation - service release and permission management

Explain spark operation mode in detail (local+standalone+yarn)

Cookie&Session

C语言的sem_t变量类型

Learning notes for introduction to C language multithreaded programming

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

后台系统右边内容如何出现滚动条和解决双滚动条的问题

家居网购项目

RSN:Learning to Exploit Long-term Relational Dependencies in Knowledge Graphs

数据库中COMMENT关键字的使用
随机推荐
About the application of MySQL
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
实现pow(x,n)函数
数据交换 JSON
Nacos
Clion and C language
雪崩问题以及sentinel的使用
C语言的sem_t变量类型
Ouc2021 autumn - Software Engineering - end of term (recall version)
在线公网安备案保姆级教程【伸手党福利】
Md5sum operation
服务器渲染技术jsp
How to achieve 0 error (s) and 0 warning (s) in keil5
[us match preparation] complete introduction to word editing formula
复习专栏之---消息队列
Pytest -- plug-in writing
pytorch训练深度学习网络设置cuda指定的GPU可见
5、【WebGIS实战】软件操作篇——服务发布及权限管理
【伸手党福利】开发人员重装系统顺序
pytest-fixture