This blog post explores the classic coin change problem: determining the minimum number of coins needed to represent a given amount of money using a set of available coin denominations. We’ll tackle this problem using dynamic programming in Python, providing clear explanations and code examples to illustrate the solution.
Understanding the Problem
Imagine you have a set of coin denominations (e.g., 1, 5, 10, 25 cents) and you need to make change for a specific amount (e.g., 37 cents). The goal is to find the minimum number of coins required to reach that amount. Using only 1-cent coins would require 37 coins, but a more efficient solution would likely involve a combination of larger denomination coins.
Dynamic Programming Approach
Dynamic programming is a powerful technique to solve optimization problems by breaking them down into smaller overlapping subproblems. We’ll build a table (a list in Python) to store the minimum number of coins needed for each amount from 0 up to the target amount.
The approach involves:
Initialization: Create a table
dp
wheredp[i]
stores the minimum number of coins needed to make change for amounti
. Initializedp[0]
to 0 (no coins needed for amount 0). Other entries are initially set to infinity (representing an unreachable amount).Iteration: Iterate through each coin denomination. For each coin, update the
dp
table. If a coinc
can be used to reach amounti
(i.e.,i - c >= 0
), then the minimum number of coins for amounti
is the minimum between the current value ofdp[i]
and1 + dp[i - c]
(one coinc
plus the minimum number of coins for the remaining amounti - c
).Result: After iterating through all coins,
dp[target_amount]
will contain the minimum number of coins needed to make change for the target amount.
Python Code Example
Here’s a Python function implementing this dynamic programming approach:
def min_coins(coins, target_amount):
"""
Finds the minimum number of coins needed to make change for a given amount.
Args:
coins: A list of available coin denominations.
target_amount: The target amount to make change for.
Returns:
The minimum number of coins, or -1 if the amount is unreachable.
"""
= [float('inf')] * (target_amount + 1) # Initialize dp table
dp 0] = 0 # Base case: 0 coins needed for amount 0
dp[
for coin in coins:
for i in range(coin, target_amount + 1):
= min(dp[i], 1 + dp[i - coin])
dp[i]
return dp[target_amount] if dp[target_amount] != float('inf') else -1
# Example usage:
= [1, 5, 10, 25]
coins = 37
target_amount = min_coins(coins, target_amount)
min_num_coins
if min_num_coins != -1:
print(f"Minimum number of coins needed for {target_amount}: {min_num_coins}")
else:
print(f"Cannot make change for {target_amount} with the given coins.")
This code efficiently calculates the minimum number of coins. The use of float('inf')
handles cases where a certain amount is unreachable with the given coin denominations. The example demonstrates how to use the function and interpret the results. This dynamic programming solution provides an optimal answer, guaranteeing the minimum number of coins.