Day44|leetcode 518. Change Exchange II, 377. Combination Sum IV

Complete Backpack Theoretical Basis

Video link: Take you to learn the complete knapsack problem! What is the difference with the 01 backpack? What is the importance of traversal order? _哔哩哔哩_bilibili

The difference between the complete backpack and the 01 backpack is that each item in the 01 backpack can only be taken once, while each item in the full backpack can be taken countless times.

leetcode 518. Change Exchange II

Topic Link: 518. Change Exchange II - LeetCode

Video Link: Dynamic Programming Complete Backpack, How Many Ways to Fill a Backpack? Combination and arrangement are exquisite! | LeetCode: 518. Change Exchange II_哔哩哔哩_bilibili

topic overview

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.

Example 1:

   Input: amount = 5, coins = [1, 2, 5]

   output: 4

Explanation: There are four ways to round up the total amount:

   5=5

   5=2+2+1

   5=2+1+1+1

   5=1+1+1+1+1

Example 2:

   Input: amount = 3, coins = [2]

   output: 0

   Explanation: Total amount 3 cannot be made using only coins of denomination 2.

Example 3:

   Input: amount = 10, coins = [10]

   output: 1

train of thought

This question does not emphasize the order of elements, just like in Example 1, {221} and {122} are a combination rather than two, so this question is looking for the number of combinations (the order between elements is not emphasized). This question is a complete backpack, because items can be taken unlimited times.

It's still a five-part series

1. Determine the dp array and the meaning of the subscript

dp[j]: The number of currency combinations that make up the total amount j is dp[j].

2. Determine the recursive formula

dp[j] += dp[j - coins[i]] (There are several ways to fill the backpack, the formula is: dp[j] += dp[j - nums[i]], the formula of this question is based on this derived from formulaic reasoning).

3. dp array initialization

dp[0] = 1。

4. Determine the traversal order

Although it is said that in the complete backpack, the two layers of for loops can be reversed, but this question does not work. The first layer of the for loop traverses the money (items), and the second layer of the for loop traverses the total amount (backpacks). There is no need to specifically emphasize the content of the complete backpack. Layer loops must be traversed from back to front. The reason why this question has an order is because what we are asking for is the number of combinations. Once the order is reversed, it becomes the number of permutations.

5. Print dp array

518. Changing Change II

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];

    }
};

leetcode 377. Combination sum Ⅳ

Topic Link: 377. Combination Sum IV - LeetCode

Video link: Dynamic Programming Complete Backpack, How many ways are there to fill up the backpack? Find the number of permutations? | LeetCode: 377. Combined Sum IV_哔哩哔哩_bilibili

topic overview

You are given an   array of  distinctnums integers  , and a target integer  target . nums Please   find out and return target the number of element combinations whose sum is .

The question data guarantees that the answer fits within the range of 32-bit integers.

Example 1:

Input: nums = [1,2,3], target = 4
 Output: 7
 Explanation:
All possible combinations are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
Note that sequences with different orders are considered different combinations.

Example 2:

Input: nums = [9], target = 3
 Output: 0

train of thought

This question is actually similar to the previous question, the only difference is that this question is in order, and the number of permutations is required, that is, the two layers of for loops are reversed in the traversal order, and the rest are almost unchanged.

It's still a five-part series

1. Determine the dp array and the meaning of the subscript

dp[i]: The number of permutations whose target positive integer is i is dp[i].

2. Determine the recursive formula

dp[i] += dp[i - nums[j]]。

3. dp array initialization

dp[0] = 1。

4. Determine the traversal order

The first layer of for loop traverses the backpack, and the second layer of for loop traverses the items.

5. Print dp array

377. Combined sum Ⅳ

 

Code

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

    }
};

Guess you like

Origin blog.csdn.net/m0_74583479/article/details/132467368