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
.
You may assume that you have an infinite number of each kind of coin.
The answer is guaranteed to fit into a signed 32-bit integer.
Example 1:
Input: amount = 5, coins = [1,2,5] Output: 4 Explanation: there are four ways to make up the 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 amount of 3 cannot be made up just with coins of 2.
Example 3:
Input: amount = 10, coins = [10] Output: 1
Constraints:
1 <= coins.length <= 300
1 <= coins[i] <= 5000
coins
are unique.0 <= amount <= 5000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find how many different combinations of coins add up to a target amount. The brute force approach tries every single possible combination of coins, no matter how inefficient, to see if it reaches the target.
Here's how the algorithm would work step-by-step:
def coin_change_brute_force(amount, coins):
number_of_combinations = 0
def find_combinations(current_amount, coin_index):
nonlocal number_of_combinations
# If current_amount equals to amount, increment the counter
if current_amount == amount:
number_of_combinations += 1
return
# If current_amount exceeds the amount, return
if current_amount > amount:
return
# If we have used all the coins, return
if coin_index >= len(coins):
return
# Explore combinations including the current coin
find_combinations(current_amount + coins[coin_index], coin_index)
# Explore combinations excluding the current coin
find_combinations(current_amount, coin_index + 1)
# Start with no coins and the first coin type
find_combinations(0, 0)
return number_of_combinations
The goal is to find the number of ways to make change for a certain amount of money using different coin denominations. Instead of brute-forcing every possible combination, we'll use a clever method to build up the solution piece by piece.
Here's how the algorithm would work step-by-step:
def coin_change_two(amount, coins):
number_of_coins = len(coins)
# dp[i][j] stores the number of ways to make amount j with first i coins
dp_table = [[0] * (amount + 1) for _ in range(number_of_coins + 1)]
# Base case: There's one way to make amount 0 (no coins).
for i in range(number_of_coins + 1):
dp_table[i][0] = 1
for i in range(1, number_of_coins + 1):
for current_amount in range(1, amount + 1):
coin_value = coins[i - 1]
# If the current coin's value is <= current amount.
if coin_value <= current_amount:
# Consider using the current coin or not using it.
dp_table[i][current_amount] = dp_table[i - 1][current_amount] + dp_table[i][current_amount - coin_value]
# If current coin's value > current amount.
else:
# Exclude the current coin; can't use it.
dp_table[i][current_amount] = dp_table[i - 1][current_amount]
# The answer is at dp[number_of_coins][amount]
return dp_table[number_of_coins][amount]
Case | How to Handle |
---|---|
coins array is null or empty | Return 0 if the coins array is null or empty, as no combinations are possible. |
amount is negative | Return 0 if the amount is negative, as it's impossible to make negative change. |
amount is 0 | Return 1 if the amount is 0, as there's one way to make change (using no coins). |
coins array contains zero or negative values | Filter out any non-positive coin values before processing to avoid invalid combinations or infinite loops. |
Large amount and large coin values can lead to integer overflow | Use a data type with larger capacity like 'long' to store the number of combinations to prevent overflow. |
Very large coins array size | The DP table size will depend only on the amount, the time complexity will still increase depending on coin size. |
No combination sums up to the amount | The algorithm returns 0 if no combination of coins sums up to the target amount after the DP table is fully processed. |
Coins array contains duplicate values | The dynamic programming approach inherently handles duplicate coin values correctly as they are simply different paths to the same total. |