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 <= 3001 <= coins[i] <= 5000coins are unique.0 <= amount <= 5000When 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:
Imagine you have a target amount of money and a collection of coin values, and you want to find every possible combination of those coins that adds up to the exact target. The brute force approach explores every single combination of coins possible to see which ones successfully reach the target amount.
Here's how the algorithm would work step-by-step:
class Solution:
def change(self, target_amount: int, coin_values: list[int]) -> int:
# We use a recursive helper function to explore every possible coin combination path.
def explore_combinations(current_index, remaining_target):
if remaining_target == 0:
return 1
if remaining_target < 0 or current_index == len(coin_values):
return 0
include_current_coin = explore_combinations(current_index, remaining_target - coin_values[current_index])
exclude_current_coin = explore_combinations(current_index + 1, remaining_target)
return include_current_coin + exclude_current_coin
return explore_combinations(0, target_amount)Instead of exploring every possible combination of coins at once, the optimal approach builds up the solution gradually by tackling one coin type at a time. By accumulating the number of ways to reach every target amount step by step, we avoid repeating work and efficiently find the total number of combinations.
Here's how the algorithm would work step-by-step:
def change(target_amount, coin_denominations):
# Initialize table to store ways to form each amount, starting with zero amount.
ways_to_reach_amount = [0] * (target_amount + 1)
ways_to_reach_amount[0] = 1
# Process each coin independently to build combinations iteratively.
for current_coin in coin_denominations:
for running_amount in range(current_coin, target_amount + 1):
# Accumulate combinations by adding the ways to form the remainder amount.
ways_to_reach_amount[running_amount] += ways_to_reach_amount[running_amount - current_coin]
return ways_to_reach_amount[target_amount]| Case | How to Handle |
|---|---|
| Target amount is zero | Return one because there is exactly one way to make zero amount by using no coins. |
| Coin denominations are larger than the target amount | The dynamic programming table will correctly retain a count of zero for the target since no combination can be formed. |
| Empty coins array with a positive target amount | Return zero immediately because no coins are available to form the target. |
| Coins array contains duplicate denominations | The nested iteration processes each coin value independently, so duplicates naturally contribute properly without causing overcounting. |
| Maximum target amount and large coin arrays | Use a 1D dynamic programming array with 64-bit integers to prevent integer overflow and ensure efficient scaling. |
| Coins array contains a zero denomination | Filter out zero values or handle them carefully to prevent infinite loops in coin selection logic. |
| Coins array contains negative numbers | Pre-screen and ignore negative denominations because coin values must be strictly positive to avoid infinite combinations. |
| Single coin denomination that exactly divides the target | The algorithm will increment the combination count at the target index exactly once when that single coin is repeatedly applied. |