Algorithm Learning|Dynamic Programming LeetCode Complete Backpack, 518. Change Exchange II, 377. Combination Sum IV

1. Complete Backpack

Complete Backpack: An item can be used countless times.
01 Backpack: An item can only be used once
. Pure Complete Backpack: When the backpack is filled, what is the maximum value of it?

Traversal order: Positive order traversal can add items multiple times, traversing items first and then traversing the backpack (the order of the pure complete backpack for loop can be reversed)

// 先遍历物品,在遍历背包
void test_CompletePack() {
    
    
    vector<int> weight = {
    
    1, 3, 4};
    vector<int> value = {
    
    15, 20, 30};
    int bagWeight = 4;
    vector<int> dp(bagWeight + 1, 0);
    for(int i = 0; i < weight.size(); i++) {
    
     // 遍历物品
        for(int j = weight[i]; j <= bagWeight; j++) {
    
     // 遍历背包容量
            dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
        }
    }
    cout << dp[bagWeight] << endl;
}
int main() {
    
    
    test_CompletePack();
}

// 先遍历背包,再遍历物品
void test_CompletePack() {
    
    
    vector<int> weight = {
    
    1, 3, 4};
    vector<int> value = {
    
    15, 20, 30};
    int bagWeight = 4;

    vector<int> dp(bagWeight + 1, 0);

    for(int j = 0; j <= bagWeight; j++) {
    
     // 遍历背包容量
        for(int i = 0; i < weight.size(); i++) {
    
     // 遍历物品
            if (j - weight[i] >= 0) dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
        }
    }
    cout << dp[bagWeight] << endl;
}
int main() {
    
    
    test_CompletePack();
}

2. Change Exchange||

Coins of different denominations and a total amount are given. Write a function to count the number of combinations of coins that make up the total amount. Suppose there are an infinite number of coins of each denomination.

train of thought

How many ways are there to fill this collection (total amount)?
1. dp[j]: There is dp[j] method for filling up the knapsack capacity j.
2. Recursive formula: dp[j] += dp[ j - coins[i ]]
3. Initialization: dp[0] = 1 and other non-zero subscripted dp arrays are initialized to 0
4. Traversing order: only (1,2) (that is, the number of combinations) will appear when traversing the items
first and then traversing the backpack Items have both (1,2) and (2,1) (i.e. number of permutations)

Implementation code

class Solution {
    
    
public:
    int change(int amount, vector<int>& coins) {
    
    
        vector<int> dp(amount + 1, 0);
        dp[0] = 1;
        for(int i = 0; i < coins.size(); i++) {
    
     //遍历物品
            for(int j = coins[i]; j <= amount; j++) {
    
     //遍历背包
                dp[j] += dp[j - coins[i]];
            }
        }
        return dp[amount];
    }
};

3. Combined sum IV

Given an array of positive integers with no duplicate numbers, find the number of combinations that sum to a given target positive integer.

train of thought

Emphasize the order of elements: 112 and 211 are two combinations

Implementation code

class Solution {
    
    
public:
    int combinationSum4(vector<int>& nums, int target) {
    
    
        vector<int> dp(target + 1, 0);
        dp[0] = 1;
        for(int j = 0; j <= target; j++) {
    
     // 遍历背包
            for(int i = 0 ; i < nums.size(); i++) {
    
     //遍历物品
            if(j - nums[i] >= 0 && dp[j] < INT_MAX - dp[j - nums[i]]){
    
    
                dp[j] += dp[j - nums[i]];
            }
            }
        }
        return dp[target];
    }
};

Four. Summary

How many ways are there to find the full backpack:
if it is to find the number of combinations, the outer for loop traverses the items, and the inner for loop traverses the backpack
If it is to find the number of permutations, the outer for loop traverses the backpack, and the inner for loop traverses the items

Guess you like

Origin blog.csdn.net/li26324949/article/details/129912508