Code Caprice Training Camp day44| 518. Change Exchange II 377. Combination Sum IV

@TOC


foreword

Code Random Record Algorithm Training Camp day44


1. Leetcode 518. Change Exchange II

1. Topic

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

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

Suppose there are an infinite number of coins of each denomination.

The title data guarantees that the result fits into a 32-bit signed integer.

Example 1:

Input: amount = 5, coins = [1, 2, 5] Output: 4 Explanation: There are four ways to make 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: The total amount 3 cannot be made up only with coins of denomination 2.

Example 3:

Input: amount = 10, coins = [10] Output: 1

hint:

1 <= coins.length <= 300
1 <= coins[i] <= 5000
coins 中的所有值 互不相同
0 <= amount <= 5000

Source: LeetCode Link: https://leetcode.cn/problems/coin-change-ii

2. Problem-solving ideas

Method 1: Dynamic Programming

In this question, given the total amount amountamount and the array coinscoins, it is required to calculate the number of coin combinations whose sum is equal to amountamount. Among them, each element of coinscoins can be selected multiple times, regardless of the order of the selected elements, so this question needs to calculate the number of combinations of selected coins.

The number of possible combinations can be calculated by means of dynamic programming. Use dp[x]dp[x] to represent the number of coin combinations whose sum is equal to xx, and the goal is to find dp[amount]dp[amount].

The bounds of dynamic programming are dp[0]=1dp[0]=1. The amounts sum to 00 only when no coins are picked, so there are only 11 coin combinations.

For coins with a denomination of coincoin, when coin≤i≤amountcoin≤i≤amount, if there is a coin combination whose sum is equal to i−coini−coin, then add a coin with a denomination of coincoin to the coin combination, A combination of coins whose sum is equal to ii can be obtained. Therefore, it is necessary to traverse coinscoins, and for each denomination of coins among them, update the value of each element greater than or equal to the denomination in the array dpdp.

This leads to dynamic programming:

初始化 dp[0]=1dp[0]=1;

遍历 coinscoins,对于其中的每个元素 coincoin,进行如下操作:
    遍历 ii 从 coincoin 到 amountamount,将 dp[i−coin]dp[i−coin] 的值加到 dp[i]dp[i]。

最终得到 dp[amount]dp[amount] 的值即为答案。

The above approach does not double count different permutations. Because the outer loop is traversing the value of the array coinscoins, the inner loop is traversing the sum of different amounts. When calculating the value of dp[i]dp[i], it can be ensured that the sum of the amount is equal to the order of the coin denomination of ii, because The order is determined, so different permutations are not double-counted.

For example, coins=[1,2]coins=[1,2], for the calculation of dp[3]dp[3], it must first traverse the coin denomination 11 and then traverse the coin denomination 22, only the following 22 combinations will appear:

3=1+1+13=1+233​=1+1+1=1+2​

It is impossible for coin denomination 22 to appear before coin denomination 11, ie there is no double counting of 3=2+13=2+1.

3. Code implementation

```java class Solution { public int change(int amount, int[] coins) { int[] dp = new int[amount + 1]; dp[0] = 1; for (int coin : coins) { for (int i = coin; i <= amount; i++) { dp[i] += dp[i - coin]; } } return dp[amount]; } }

```

2. Leetcode 377. Combined sum Ⅳ

1. Topic

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

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 treated as different combinations.

Example 2:

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

hint:

1 <= nums.length <= 200
1 <= nums[i] <= 1000
nums 中的所有元素 互不相同
1 <= target <= 1000

Source: LeetCode Link: https://leetcode.cn/problems/combination-sum-iv

2. Problem-solving ideas

Method 1: Dynamic Programming

In this question, given the array numsnums and the target value targettarget, it is required to calculate the number of solutions to select several elements from numsnums so that their sum is equal to targettarget. Among them, each element of numsnums can be selected multiple times, and the order of selecting elements needs to be considered. Since the order of the selected elements needs to be considered, what needs to be calculated in this question is the number of permutations of the selected elements.

The number of possible solutions can be calculated by means of dynamic programming. Use dp[x]dp[x] to represent the number of solutions whose sum of selected elements is equal to xx, and the goal is to find dp[target]dp[target].

The bounds of dynamic programming are dp[0]=1dp[0]=1. The sum of the elements is 00 only when no element is selected, so there are only 11 options.

When 1≤i≤target1≤i≤target, if there is a permutation in which the sum of the elements is equal to ii, then the last element of the permutation must be an element in the array numsnums. Assuming that the last element of the arrangement is numnum, there must be num≤inum≤i. For each arrangement whose sum of elements is equal to i−numi−num, after adding numnum at the end, we can get a sum of elements equal to ii permutations, so when calculating dp[i]dp[i], the sum of all dp[i−num]dp[i−num] should be calculated.

This leads to dynamic programming:

初始化 dp[0]=1dp[0]=1;

遍历 ii 从 11 到 targettarget,对于每个 ii,进行如下操作:
    遍历数组 numsnums 中的每个元素 numnum,当 num≤inum≤i 时,将 dp[i−num]dp[i−num] 的值加到 dp[i]dp[i]。

最终得到 dp[target]dp[target] 的值即为答案。

Does the above approach take into account the order in which elements are selected? The answer is yes. Because the outer loop traverses the values ​​from 11 to targettarget, and the inner loop traverses the values ​​of the array numsnums, when calculating the value of dp[i]dp[i], each element in numsnums less than or equal to ii may be used as The sum of the elements is equal to the last element of the permutation of ii. For example, both 11 and 33 are in the array numsnums, when calculating dp[4]dp[4], the last element of the array can be 11 or 33, so dp[1]dp[1] and dp[3] dp[3] will be considered, that is, different orders will be considered.

3. Code implementation

```java class Solution { public int combinationSum4(int[] nums, int target) { int[] dp = new int[target + 1]; dp[0] = 1; for (int i = 1; i <= target; i++) { for (int num : nums) { if (num <= i) { dp[i] += dp[i - num]; } } } return dp[target]; } }

```

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131285407