当前位置:网站首页>Record of brushing questions with force deduction -- complete knapsack problem (I)
Record of brushing questions with force deduction -- complete knapsack problem (I)
2022-07-06 15:40:00 【Xiaogeng who loves learning】
List of articles
Complete knapsack problem ( One )
Tips : After writing the article , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
Preface
Tips : Here you can add the general content to be recorded in this article :
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 Introduction to the complete knapsack problem
The complete knapsack problem is actually 0-1 Based on the knapsack problem , Added feature that each item can be selected multiple times ( Where capacity permits ).
Two 、 Change for (Leetcode 322 secondary )
1. Problem description
Give you an array of integers coins , Coins of different denominations ; And an integer amount , Represents the total amount . Calculate and return the amount needed to make up the total amount The minimum number of coins . If no combination of coins can make up the total amount , return -1 . You can think of the number of coins of each kind as infinite .
2. Description of algorithm
2.1 The basic idea of the complete knapsack problem
Complete knapsack problem and 0-1 The essence of knapsack problem is the same , It's just that the conditions are different . Now I will analyze it according to the code . The following code is the general method to solve the complete knapsack problem . and 0-1 The difference between the knapsack problem is : When considering the current coin situation, you can choose multiple times , Maximum number of selections = Current backpack allowance / The face value of the current coin .
class Solution {
int INF = Integer.MAX_VALUE;
public int coinChange(int[] cs, int cnt) {
int n = cs.length;
int[][] f = new int[n + 1][cnt + 1];
// initialization ( There are no coins ): Only f[0][0] = 0; Other cases are invalid values .
// This is from 「 State definition 」 Decisive , When not considering any coins , You can only add up to 0 The plan , The number of coins used is 0
for (int i = 1; i <= cnt; i++) f[0][i] = INF;
// With coins
for (int i = 1; i <= n; i++) {
int val = cs[i - 1];
for (int j = 0; j <= cnt; j++) {
// Regardless of the current coin situation
f[i][j] = f[i - 1][j];
// Consider the current coin situation ( The current number of coins can be selected based on the current capacity )
for (int k = 1; k * val <= j; k++) {
if (f[i - 1][j - k * val] != INF) {
f[i][j] = Math.min(f[i][j], f[i-1][j-k*val] + k);
}
}
}
}
return f[n][cnt] == INF ? -1 : f[n][cnt];
}
}
2.2 Analysis of specific problems
This problem can be transformed into a complete knapsack problem , Problem modeling process : How big is the volume of the backpack 、 The quantity and value of items 、 The form of the result
1. Number of items :nums Of size
2. The value of the goods :nums The corresponding value
3. The volume of the backpack : Backpack size
4. Initial value : This is also the difficulty of many knapsack problems , The so-called initial value here , does dp[0]、dp[1]…dp[n] The meaning of representation . If you want to determine the initial value, you should determine which values are invalid according to the meaning of the topic , Which values need to be transferred .
5. The form of the result : The problem of the topic is how to calculate and return the minimum number of coins needed to make up the total amount . The original solution of knapsack problem is to find the solution with the greatest value . But now we need to be flexible , In essence, it is still analyzing the state transition equation . Namely dp[j] and dp[j - v[i]] The relationship between , Here is the coin , The last thing I want is that the minimum number of coins is not the maximum value , therefore dp[j] and dp[j - v[i]] Namely dp[j]=min(dp[j],dp[j - v[i]]+1);
6. The final result is : You need to judge which values are valid values according to the initial values , Which are invalid values .
2.4 Code display :
class Solution {
private:
int INF = 0x3f3f3f3f;
public:
int coinChange(vector<int>& coins, int amount) {
int C=coins.size();
vector<int> dp(amount+1,INF);
dp[0]=0;
if(amount==0) return 0;
for(int i=0;i<C;i++)
{
for(int j=coins[i];j<=amount;j++)
{
int yes=dp[j-coins[i]]+1;
dp[j]=min(dp[j],yes);
}
}
return dp[amount]==INF?-1:dp[amount];
}
};
summary
Tips : Here is a summary of the article :
for example : That's what we're going to talk about today , This article only briefly introduces 「 What is the complete knapsack problem 」、「 What is the essence of the knapsack problem 」 as well as 「 Why is the conventional solution of complete knapsack problem and the difficulty of solving routine and deformation problems of complete knapsack , More will be introduced later according to the examples .
边栏推荐
- STM32学习记录:输入捕获应用
- C语言数组的概念
- Research Report on printed circuit board (PCB) connector industry - market status analysis and development prospect forecast
- ucore lab 2
- 0-1背包問題(一)
- 0-1 knapsack problem (I)
- Cost accounting [20]
- LeetCode#412. Fizz Buzz
- Introduction to safety testing
- Learning record: STM32F103 clock system overview working principle
猜你喜欢

VS2019初步使用

UCORE LaB6 scheduler experiment report

FSM and I2C experiment report

用C语言写网页游戏

Word macro operation: convert the automatic number in the document into editable text type

学习记录:理解 SysTick系统定时器,编写延时函数

ucorelab3

What are the commonly used SQL statements in software testing?

Learning record: how to perform PWM output

学习记录:STM32F103 时钟系统概述工作原理
随机推荐
JS --- BOM details of JS (V)
Research Report on pharmaceutical R & D outsourcing service industry - market status analysis and development prospect forecast
Leetcode notes - dynamic planning -day6
Cost accounting [13]
What are the software testing methods? Show you something different
C语言数组的概念
LeetCode#62. Different paths
Learning record: understand systick system timer and write delay function
学习记录:使用STM32F1看门狗
Your wechat nickname may be betraying you
Intensive learning notes: Sutton book Chapter III exercise explanation (ex17~ex29)
Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
Want to change jobs? Do you know the seven skills you need to master in the interview software test
毕业才知道IT专业大学生毕业前必做的1010件事
ucorelab4
LeetCode#198. raid homes and plunder houses
Automated testing problems you must understand, boutique summary
C语言学习笔记
Cost accounting [17]
Cost accounting [13]