[01 Backpack Theory] 01 Backpack Problem dp[j]
There are n items and a backpack that can carry a maximum weight of w.
The weight of the i-th item is weight[i], and the obtained value is value[i].
There is only one item of each item. Find out which items should be put into the backpack and the total value of the items is the largest.
answer
dynamic programming
-
Determine the meaning of the dp array and subscripts
. Rolling arraydp[j]
:j
a backpack with a capacity of can carry items with a maximum value ofdp[j]
-
Determine the recursive formula
dp[j]
to bej
the maximum value carried by a backpack with a capacity of :
dp[j]
It candp[j - weight[i]]
be deduceddp[j - weight[i]]
to represent thej - weight[i]
maximum value carried by a backpack with a capacity of .dp[j - weight[i]] + value[i]
Representsj - 物品i重量
a backpack with a capacity plusi
the value of the items. (That is, the value ofj
a backpack with a capacity of , after putting items in is: ) There are two options:i
dp[j]
dp[j]
- One is to take itself,
dp[j]
which is equivalent to the two-dimensional dp arraydp[i-1][j]
, that is, no items are placedi
(same as last time). - One is to take
dp[j - weight[i]] + value[i]
, that is, put itemsi
, and specify the maximum value. After all, it is to seek the maximum value.
- One is to take itself,
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
- How to initialize dp array
dp[j]
Represents: For j
a backpack with a capacity of , the value of the items carried can be up to . dp[j]
First, dp[0]=0.
When deriving the dp array, the number with the largest value must be taken. If the values given in the question are all positive integers, then the subscript is not 0. Just initialize them all to 0, that is, the values of other subscripts are all initialized to 0, so that the dp array can take the maximum value during the recursive formula process instead of being overwritten by the initial value.
- Determine the order of traversal.
When traversing two-dimensional dp, the backpack capacity is from small to large. When traversing one-dimensional dp, the backpack capacity is from large to small.
Items are first placed in forward order and then backpacked in reverse order. The purpose of reverse order traversal is to ensure that itemsi
are only put in once. If traversed in positive order, item 0 will be added multiple times.
for(int i = 0; i < weight.size(); i++) {
// 遍历物品
for(int j = bagWeight; j >= weight[i]; j--) {
// 遍历背包容量
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
}
}
- Take an example to deduce the dp array (print the dp array) and
the maximum weight of the backpack is 4. thing:
weight | value | |
---|---|---|
Item 0 | 1 | 15 |
Item 1 | 3 | 20 |
Item 2 | 4 | 30 |
public class Solution {
public static void main(String[] args) {
int[] weight = {
1, 3, 4};
int[] value = {
15, 20, 30};
int bagWight = 4;
testWeightBagProblem(weight, value, bagWight);
}
public static void testWeightBagProblem(int[] weight, int[] value, int bagWeight){
int wLen = weight.length;
//定义dp数组:dp[j]表示背包容量为 j 时,能获得的最大价值
int[] dp = new int[bagWeight + 1];
//遍历顺序:先遍历物品,再遍历背包容量(倒序)
for (int i = 0; i < wLen; i++){
for (int j = bagWeight; j >= weight[i]; j--){
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]);
}
}
//打印dp数组
for (int j = 0; j <= bagWeight; j++){
System.out.print(dp[j] + " ");
}
}
}