LeetCode dynamic programming for change exchange II

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

topic

Change Exchange II

You are given an array of coinsintegers for the coins of different denominations, and an integer amountfor the total amount.

Please calculate and return the number of coin combinations that make up the total amount. Returns 0 if no combination of coins can make up the total.

Suppose there are infinite coins of each denomination. 

The title data guarantees that the result conforms 32to a signed integer.

Example 1:

输入:amount = 5, coins = [1, 2, 5]
输出:4
解释:有四种方式可以凑成总金额:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1
复制代码

Example 2:

输入:amount = 3, coins = [2]
输出:0
解释:只用面额 2 的硬币不能凑成总金额 3 。
示例 3:

输入:amount = 10, coins = [10] 
输出:1
复制代码

answer

problem-solving analysis

Problem solving ideas:

  1. The longest common subsequence problem is a typical two-dimensional dynamic programming problem.
  2. Question retelling: In this question, the total amount is given as the amount, and the array coins are required to calculate the amount only and the number of coin combinations equal to the amount. Among them, the American and Russian elements in coins can be selected multiple times, regardless of the order of the elements, so this topic is to calculate the number of combinations of coins selected .
  3. Topic analysis: We can calculate the number of groups by dynamic programming. Use dp[x] to represent the amount only the number of combinations equal to x, and then find dp[amount].
  • The dynamic programming boundary value dp[0] = 1. The sum of amounts is 0 only when no coins are selected, so there is only 1 coin combination.
  • For coins of denomination coin, only when coin <= i <= annont, if there is a coin combination whose sum is equal to i - coin, add a coin of denomination coin to the coin combination, that is You can get a combination of coins with an amount only equal to i. Hence it is necessary to iterate over coins for each denomination. , update the value of each heavy rain or element equal to the denomination in the array dp.
  • Summarize the process of dynamic programming
      1). Initialize dp[0] = 1;
      2). Traverse coins, and for each element coin in it, do the following: - Traverse i from coin to amount, convert dp[i-coin] The value is accumulated to dp[i]
      3). The final value of dp[amount] is obtained.
  1. Consideration and attention: Whether there will be a double-counting problem for the above process. No, because the outer loop traverses the value of coins, and the inner loop traverses different amounts and sums. When calculating the value of dp[i], it can ensure that the amount is only the sum of i. Since the order is determined, Therefore there will be no repeated permutations.

Complexity:
Time Complexity: O(M*N)
Space Complexity:O(M)

problem solving code

The solution code is as follows (detailed comments in the code):

int change(int amount, int* coins, int coinsSize){
        // 数组
        int dp[amount +1];
        // 数组初始化
        memset(dp, 0, sizeof(dp));
        // 初始化第一个值
        dp[0] = 1;
        // 所有的零钱
        for (int i = 0; i < coinsSize; i++) {
               // 零钱组合
               for (int j = coins[i]; j <= amount; j++) { 
                   dp[j] += dp[j - coins[i]];
               }
        }
        return dp[amount];
}
复制代码

Feedback results after submission (because this topic has not been optimized, the performance is average):

image.png

Reference Information

Guess you like

Origin juejin.im/post/7084609270540402696