|
| 1 | +--- |
| 2 | +id: coin-change-2 |
| 3 | +title: Coin Change 2 |
| 4 | +sidebar_label: 0518-Coin-Change-2 |
| 5 | +tags: |
| 6 | +- Dynamic Programming |
| 7 | +- Array |
| 8 | +description: "You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the number of combinations that make up that amount." |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money. 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`. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +**Input:** `amount = 5, coins = [1, 2, 5]` |
| 20 | +**Output:** `4` |
| 21 | +**Explanation:** There are four ways to make up the amount: |
| 22 | +1. 5 = 5 |
| 23 | +2. 5 = 2 + 2 + 1 |
| 24 | +3. 5 = 2 + 1 + 1 + 1 |
| 25 | +4. 5 = 1 + 1 + 1 + 1 + 1 |
| 26 | + |
| 27 | +**Example 2:** |
| 28 | + |
| 29 | +**Input:** `amount = 3, coins = [2]` |
| 30 | +**Output:** `0` |
| 31 | +**Explanation:** The amount of 3 cannot be made up just with coins of 2. |
| 32 | + |
| 33 | +**Example 3:** |
| 34 | + |
| 35 | +**Input:** `amount = 10, coins = [10]` |
| 36 | +**Output:** `1` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `1 <= coins.length <= 300` |
| 41 | +- `1 <= coins[i] <= 5000` |
| 42 | +- All the values of `coins` are unique. |
| 43 | +- `0 <= amount <= 5000` |
| 44 | + |
| 45 | +--- |
| 46 | + |
| 47 | +## Approach |
| 48 | + |
| 49 | +To solve this problem, we can use dynamic programming. We will create a 1D array `dp` where `dp[i]` represents the number of ways to make the amount `i` using the given coins. |
| 50 | + |
| 51 | +### Steps: |
| 52 | + |
| 53 | +1. Initialize a 1D array `dp` of size `amount + 1` with all elements set to `0`. |
| 54 | +2. Set `dp[0] = 1` because there is one way to make the amount `0`, which is to use no coins. |
| 55 | +3. For each coin in the `coins` array: |
| 56 | + - Update the `dp` array from the current coin's value up to the `amount`. |
| 57 | + - For each amount `i` from the coin's value to `amount`, add the number of ways to make the amount `i - coin` to `dp[i]`. |
| 58 | +4. The value `dp[amount]` will be the result. |
| 59 | + |
| 60 | +### Solution |
| 61 | + |
| 62 | +#### Java |
| 63 | + |
| 64 | +```java |
| 65 | +class Solution { |
| 66 | + public int change(int amount, int[] coins) { |
| 67 | + int[] dp = new int[amount + 1]; |
| 68 | + dp[0] = 1; |
| 69 | + |
| 70 | + for (int coin : coins) { |
| 71 | + for (int i = coin; i <= amount; i++) { |
| 72 | + dp[i] += dp[i - coin]; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return dp[amount]; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +#### C++ |
| 81 | +```cpp |
| 82 | +#include <vector> |
| 83 | +using namespace std; |
| 84 | + |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + int change(int amount, vector<int>& coins) { |
| 88 | + vector<int> dp(amount + 1, 0); |
| 89 | + dp[0] = 1; |
| 90 | + |
| 91 | + for (int coin : coins) { |
| 92 | + for (int i = coin; i <= amount; i++) { |
| 93 | + dp[i] += dp[i - coin]; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return dp[amount]; |
| 98 | + } |
| 99 | +}; |
| 100 | +``` |
| 101 | +#### Python |
| 102 | + |
| 103 | +```python |
| 104 | +class Solution: |
| 105 | + def change(self, amount: int, coins: List[int]) -> int: |
| 106 | + dp = [0] * (amount + 1) |
| 107 | + dp[0] = 1 |
| 108 | + |
| 109 | + for coin in coins: |
| 110 | + for i in range(coin, amount + 1): |
| 111 | + dp[i] += dp[i - coin] |
| 112 | + |
| 113 | + return dp[amount] |
| 114 | +``` |
| 115 | +### Complexity Analysis |
| 116 | +**Time Complexity:** O(n * amount) |
| 117 | +>Reason: The nested loops iterate over the coins array and the amount, leading to a time complexity of O(n * amount), where n is the number of coins. |
| 118 | +
|
| 119 | +**Space Complexity:** O(amount) |
| 120 | +>Reason: The space complexity is O(amount) due to the 1D dp array used to store the number of combinations for each amount. |
| 121 | +
|
| 122 | +### References |
| 123 | +**LeetCode Problem:** Coin Change 2 |
0 commit comments