[01 Backpack Theory] 01 Backpack Problem dp[j] (rolling array) <Dynamic Programming Board>

[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 array dp[j]: ja backpack with a capacity of can carry items with a maximum value ofdp[j]

  • Determine the recursive formula
    dp[j] to be jthe maximum value carried by a backpack with a capacity of :
    dp[j]It can dp[j - weight[i]]be deduced dp[j - weight[i]]to represent the j - weight[i]maximum value carried by a backpack with a capacity of . dp[j - weight[i]] + value[i]Represents j - 物品i重量a backpack with a capacity plus ithe value of the items. (That is, the value of ja backpack with a capacity of , after putting items in is: ) There are two options:idp[j]
    dp[j]

    • One is to take itself, dp[j]which is equivalent to the two-dimensional dp array dp[i-1][j], that is, no items are placed i(same as last time).
    • One is to take dp[j - weight[i]] + value[i], that is, put items i, and specify the maximum value. After all, it is to seek the maximum value.
dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
  • How to initialize dp array

dp[j]Represents: For ja 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 items iare 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

Insert image description here

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] + " ");
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132640280