|
| 1 | +# 518. Coin Change II |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Dynamic Programming. |
| 5 | +- Similar Questions: Maximum Value of K Coins From Piles, Number of Ways to Earn Points, Count of Sub-Multisets With Bounded Sum, Length of the Longest Subsequence That Sums to Target. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money. |
| 10 | + |
| 11 | +Return **the number of combinations that make up that amount**. If that amount of money cannot be made up by any combination of the coins, return `0`. |
| 12 | + |
| 13 | +You may assume that you have an infinite number of each kind of coin. |
| 14 | + |
| 15 | +The answer is **guaranteed** to fit into a signed **32-bit** integer. |
| 16 | + |
| 17 | + |
| 18 | +Example 1: |
| 19 | + |
| 20 | +``` |
| 21 | +Input: amount = 5, coins = [1,2,5] |
| 22 | +Output: 4 |
| 23 | +Explanation: there are four ways to make up the amount: |
| 24 | +5=5 |
| 25 | +5=2+2+1 |
| 26 | +5=2+1+1+1 |
| 27 | +5=1+1+1+1+1 |
| 28 | +``` |
| 29 | + |
| 30 | +Example 2: |
| 31 | + |
| 32 | +``` |
| 33 | +Input: amount = 3, coins = [2] |
| 34 | +Output: 0 |
| 35 | +Explanation: the amount of 3 cannot be made up just with coins of 2. |
| 36 | +``` |
| 37 | + |
| 38 | +Example 3: |
| 39 | + |
| 40 | +``` |
| 41 | +Input: amount = 10, coins = [10] |
| 42 | +Output: 1 |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +**Constraints:** |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +- `1 <= coins.length <= 300` |
| 51 | + |
| 52 | +- `1 <= coins[i] <= 5000` |
| 53 | + |
| 54 | +- All the values of `coins` are **unique**. |
| 55 | + |
| 56 | +- `0 <= amount <= 5000` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Solution |
| 61 | + |
| 62 | +```javascript |
| 63 | +/** |
| 64 | + * @param {number} amount |
| 65 | + * @param {number[]} coins |
| 66 | + * @return {number} |
| 67 | + */ |
| 68 | +var change = function(amount, coins) { |
| 69 | + coins.sort((a, b) => b - a); |
| 70 | + return helper(amount, coins, Array(coins.length + 1).fill(0).map(() => [])); |
| 71 | +}; |
| 72 | + |
| 73 | +var helper = function(amount, coins, dp) { |
| 74 | + if (amount === 0) return 1; |
| 75 | + if (dp[coins.length][amount] !== undefined) return dp[coins.length][amount]; |
| 76 | + var res = 0; |
| 77 | + for (var i = 0; i < coins.length; i++) { |
| 78 | + if (amount >= coins[i]) { |
| 79 | + res += helper(amount - coins[i], coins.slice(i), dp); |
| 80 | + } |
| 81 | + } |
| 82 | + dp[coins.length][amount] = res; |
| 83 | + return res; |
| 84 | +}; |
| 85 | +``` |
| 86 | +**Explain:** |
| 87 | + |
| 88 | +1. sort `coins` array, to avoid duplicate answer |
| 89 | +2. `f(5, [5,2,1]) = f(0, [5,2,1]) + f(3, [2,1]) + f(4, [1])` |
| 90 | +3. `f(0, []) = 1` |
| 91 | +4. cache every compute so that we only compute same case once |
| 92 | + |
| 93 | +**Complexity:** |
| 94 | + |
| 95 | +* Time complexity : O(n * m). |
| 96 | +* Space complexity : O(n * m). |
0 commit comments