From the end of last month began a bit impetuous, originally planned national day to check the gaps, the result network card glorious bad orz ... In any case, the offer has not yet been received, even if it is not enough to relax the learning state. Stay Hungry,stay Foolish
Knapsack problem is the classic problem of dynamic programming, Leetcode has five kinds of knapsack variant problem, now summarizes.
knapsack problem One (max. Weight)
Title Description:
Given n items with a size Ai, an integer m denotes the size of a backpack.
How can I fill this backpack?
Sample input:
[2,3,5,7],11
[2,3,5,7],12
Sample output:
Ten
12
The most common kind of knapsack problem is the maximum capacity that can be put in a backpack with a specified capacity of M
First, we introduce a method of using two-dimensional auxiliary array dp[i][j].
A two-dimensional array dp[i][j] (0<=i<a.length,0<=j<=m) represents the maximum capacity that can be placed in the case of a maximum capacity of j, which can be placed in a capacity of a[0..i], which can be divided into the following cases the maximum capacity of the backpack is less than the weight of the item. The maximum weight that can be accommodated is dp[i-1][j] the maximum capacity of the current backpack is less than the weight of the item and the maximum weight that can be accommodated is Max (Dp[i-1][j],dp[i-1][j-a[i]+a[i])
The code is as follows
public int backPack (int[] A, int m) {
int[][] dp = new INT[A.LENGTH][M + 1];
for (int j = 1; j < m + 1; j + +) {
if (j = = A[0]) {
dp[0][j] = a[0];
} else {
dp[0][j] = 0;
}
for (int i = 1; i < a.length; i++) {
if (J < A[i]) {
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = Math.max (Dp[i-1][j-a[i]] + a[i], dp[i-1][j]);
}} return dp[a.length-1][m];
}
Can actually be optimized into one-dimensional arrays
The code is as follows
public int backPack (int[] A, int m) {
int[] dp = new INT[M + 1];
for (int i = 0, i < a.length; i++) {for
(int j = m; j > 0; j--) {
if (J >= A[i]) {
Dp[j] = MATH.M Ax (Dp[j-a[i]] + a[i], dp[j-1]);
}}} return dp[m];
}
knapsack problem Two (max put value)
Title Description:
Given n items with the size Ai and value Vi, and a backpack with size M.
What's the maximum value can you put into the backpack?
Sample input
10,[2,3,5,7],[1,5,2,4]
Sample output
9
On the basis of the first class knapsack problem, each item adds value and asks for the maximum value of the items that can be put into the backpack.
Similar to the first type of problem, the difference is that the array holds the maximum value
First method of the first two-dimensional array
public int backPack (int[] A, int[] V, int m) {
int[][] dp = new INT[A.LENGTH][M + 1];
for (int j = 1; j < m + 1; j + +) {
if (j = = A[0]) {
dp[0][j] = v[0];
} else {
dp[0][j] = 0;
}
for (int i = 1; i < a.length; i++) {
if (J < A[i]) {
dp[i][j] = dp[i-1][j];
} else {
dp[i][j] = Math.max (Dp[i-1][j-a[i]] + v[i], dp[i-1][j]);
}} return dp[a.length-1][m];
}
The second Kind
public int backPack (int[] A, int[] V, int m) {
int[] dp = new INT[M + 1];
for (int i = 0, i < a.length; i++) {for
(int j = m; j > 0; j--) {
if (J >= A[i]) {
Dp[j] = MATH.M Ax (Dp[j-a[i]] + v[i], dp[j-1]);
}}} return dp[m];
}
knapsack Problem III (maximum value + repeatable selection)
Title Description
Given n kind of items with size Ai and value Vi (each item have an infinite number available) and
a backpack with size M.
What's the maximum value can you put into the backpack?
Sample input
10,[2,3,5,7],[1,5,2,4]
Sample output
15
On the basis of the second class, each item can be selected multiple times
public int backpackiii (int m, int[] A, int[] V) {
int[] dp = new INT[M + 1];
for (int i = 0, i < a.length; i++) {for
(int j = 1; j <= M; j + +) {
if (J >= A[i]) {
Dp[j] = Math.max (Dp[j], Dp[j-a[i]] + v[i]);
}} return dp[m];
}
knapsack problem Four (maximum weight + all possible results)
Title Description:
Given an integer array nums with all positive numbers and no duplicates, find the number of
possible combinations tha T add up to a positive integer target.
Sample input
[1,2,4],4
Sample output
6
On the basis of the first class, an increase in the number of possible numbers to be placed
public int backpackvi (int[] nums, int target) {
int[] dp = new Int[target + 1];
Dp[0] = 1;
for (int j = 1; J <= Target, J + +) {for
(int i = 0; i < nums.length; i++) {
if (J >= Nums[i]) {
dp[j] + = Dp[j-nums[i]]
;
}} return dp[target];
}
knapsack problem Five (maximum weight + all possible results + different permutations)
Title Description:
Given n items with size nums[i] which a integer array and all positive numbers.
An integer target denotes the size of a backpack. Find the number of possible fill the backpack.
Each item is used once
Sample input:
[1,2,3,3,7],7
Sample output:
2
In the previous case, a different sort is also considered a
public int backPackVI(int[] nums, int target) {
int[] dp = new int[target+1];
dp[0] = 1;
for (int i = 0; i < nums.length; i++) {
for (int j = target; j >= 0; j--) {
if (j >= nums[i]) dp[j] += dp[j-nums[i]];
}
}
return dp[target];
}