Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of coins needed to make the changes in Python
The coin change problem is a classic dynamic programming challenge where we need to find the minimum number of coins required to make a given amount. Given coins of denominations [1, 5, 10, 25] and a target amount, we use dynamic programming to build up solutions for smaller amounts.
Problem Understanding
For example, to make amount 64, we can use: 25 + 25 + 10 + 1 + 1 + 1 + 1 = 64, requiring 7 coins total.
Algorithm Steps
The dynamic programming approach follows these steps:
- If amount = 0, return 0 (no coins needed)
- If minimum coin value > amount, return -1 (impossible)
- Create a DP array of size (amount + 1) initialized with -1
- For each coin denomination, mark dp[coin] = 1
- For each position, calculate minimum coins using previous results
- Return dp[amount]
Implementation
class Solution:
def coinChange(self, amount):
coins = [1, 5, 10, 25]
if amount == 0:
return 0
if min(coins) > amount:
return -1
# Initialize DP array with -1 (impossible state)
dp = [-1 for i in range(amount + 1)]
# For each coin denomination
for coin in coins:
if coin > len(dp) - 1:
continue
dp[coin] = 1 # One coin needed for exact denomination
# Build up solutions for larger amounts
for j in range(coin + 1, amount + 1):
if dp[j - coin] == -1: # Previous state impossible
continue
elif dp[j] == -1: # First solution for this amount
dp[j] = dp[j - coin] + 1
else: # Choose minimum coins
dp[j] = min(dp[j], dp[j - coin] + 1)
return dp[amount]
# Test the solution
solution = Solution()
result = solution.coinChange(64)
print(f"Minimum coins needed for amount 64: {result}")
The output of the above code is ?
Minimum coins needed for amount 64: 7
How It Works
The DP array stores the minimum coins needed for each amount from 0 to target. For each coin denomination, we update all reachable amounts by adding one more coin. The algorithm ensures we always keep the minimum number of coins for each amount.
Example with Smaller Amount
class Solution:
def coinChange(self, amount):
coins = [1, 5, 10, 25]
if amount == 0:
return 0
if min(coins) > amount:
return -1
dp = [-1 for i in range(amount + 1)]
for coin in coins:
if coin > len(dp) - 1:
continue
dp[coin] = 1
for j in range(coin + 1, amount + 1):
if dp[j - coin] == -1:
continue
elif dp[j] == -1:
dp[j] = dp[j - coin] + 1
else:
dp[j] = min(dp[j], dp[j - coin] + 1)
return dp[amount]
# Test with different amounts
solution = Solution()
test_amounts = [11, 30, 64]
for amount in test_amounts:
result = solution.coinChange(amount)
print(f"Amount {amount}: {result} coins")
Amount 11: 2 coins Amount 30: 3 coins Amount 64: 7 coins
Conclusion
This dynamic programming solution efficiently finds the minimum coins needed by building up solutions from smaller amounts. The time complexity is O(amount × number of coins) and space complexity is O(amount).
