You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
You may assume that you have an infinite number of each kind of coin.
Example 1:
Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3 Output: -1
Example 3:
Input: coins = [1], amount = 0 Output: 0
Constraints:
1 <= coins.length <= 121 <= coins[i] <= 231 - 10 <= amount <= 104When 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 to make and a collection of various coin values, and you want to find the fewest coins needed to reach that exact amount. The brute force strategy works by exhaustively testing every single combination of coins to see which ones add up to the target and then picking the combination that uses the fewest coins.
Here's how the algorithm would work step-by-step:
def coin_change_brute_force(coin_values, target_amount):
# We define a recursive helper to explore every combination of coin subtractions
def recursive_explore(remaining_amount):
if remaining_amount == 0:
return 0
if remaining_amount < 0:
return float('inf')
minimum_coins_needed = float('inf')
for current_coin in coin_values:
result_from_subpath = recursive_explore(remaining_amount - current_coin)
if result_from_subpath != float('inf'):
minimum_coins_needed = min(minimum_coins_needed, result_from_subpath + 1)
return minimum_coins_needed
# We initiate the search from the full target amount down to zero
total_coins_result = recursive_explore(target_amount)
if total_coins_result == float('inf'):
return -1
return total_coins_resultInstead of blindly guessing every combination of coins, the optimal strategy builds the solution from the ground up, starting with the smallest target amount. It figures out the minimum coins needed for every smaller target first, using those answers to easily solve for the final goal.
Here's how the algorithm would work step-by-step:
def coin_change(coin_denominations, target_amount):
# We initialize with target_amount + 1 as an infinity placeholder to safely find minimums.
minimum_coins_table = [target_amount + 1] * (target_amount + 1)
minimum_coins_table[0] = 0
# We build solutions from the ground up to prevent redundant calculations.
for current_amount in range(1, target_amount + 1):
for coin_value in coin_denominations:
if current_amount >= coin_value:
# We look back at subproblems to find the optimal substructure for this amount.
previous_amount = current_amount - coin_value
potential_answer = minimum_coins_table[previous_amount] + 1
minimum_coins_table[current_amount] = min(minimum_coins_table[current_amount], potential_answer)
if minimum_coins_table[target_amount] > target_amount:
return -1
return minimum_coins_table[target_amount]| Case | How to Handle |
|---|---|
| Target amount is zero | Return zero immediately since zero coins are needed to make a total of zero. |
| No valid combination of coins can form the target amount | Return negative one as specified by the problem constraints when the target is unreachable. |
| Coins array contains values larger than the target amount | Ignore any coin whose denomination exceeds the remaining target during the state transition checks. |
| Coins array includes zero as a denomination | Skip zero values during initialization or processing to prevent infinite loops and division by zero errors. |
| Coins array contains negative numbers | Filter out negative coin values at the start since denominations must be positive. |
| Very large target amount causing integer overflow during addition | Use infinity or a value larger than the max possible coin count to represent unreachable states safely. |
| Single coin denomination equal to the target amount | Return one since the exact target can be made using a single coin of that value. |
| Maximum-sized inputs with deep target values | Use bottom-up dynamic programming with a linear array to ensure efficient scaling and prevent stack overflow. |