当前位置:网站首页>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];
}
边栏推荐
- The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
- Subnet division and subnet summary
- Depth first traversal of C implementation Diagram -- non recursive code
- Nacos
- leetcode 1482 猜猜看啊,这道题目怎么二分?
- Hal library setting STM32 interrupt
- Pytest -- plug-in writing
- FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
- Let's just say I can use thousands of expression packs
- E15 solution for cx5120 controlling Huichuan is620n servo error
猜你喜欢

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

后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动

Research on target recognition and tracking based on 3D laser point cloud

Appium自动化测试基础--补充:C/S架构和B/S架构说明

Gorilla/mux framework (RK boot): RPC error code design

JUC学习
![[nine day training] content III of the problem solution of leetcode question brushing Report](/img/7e/1e76181e56ef7feb083f9662df71c7.jpg)
[nine day training] content III of the problem solution of leetcode question brushing Report

Ridge regression and lasso regression

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

Feature Pyramid Networks for Object Detection论文理解
随机推荐
Overview of EtherCAT principle
Hal library setting STM32 interrupt
torch.histc
Learning notes for introduction to C language multithreaded programming
文件上传下载
Promise中finally的用法
FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)
Keil5中如何做到 0 Error(s), 0 Warning(s).
TEC: Knowledge Graph Embedding with Triple Context
Ouc2021 autumn - Software Engineering - end of term (recall version)
Pytorch training deep learning network settings CUDA specified GPU visible
Force buckle - sum of two numbers
multiple linear regression
Test function in pychram
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
C语言的sem_t变量类型
split(),splice(),slice()傻傻分不清楚?
GCC usage, makefile summary
Include() of array
Bilinear upsampling and f.upsample in pytorch_ bilinear