You are given an integer array coins
representing coins of different denominations and an integer amount
representing a total amount of money.
Write a function to compute 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.
coins = [1, 2, 5], amount = 11
3
(Explanation: 11 = 5 + 5 + 1)coins = [2], amount = 3
-1
(Explanation: No combination can form 3 with coin 2)coins = [1], amount = 0
0
(Explanation: 0 coins needed to make 0 amount)coins = [1], amount = 2
2
(Explanation: 1+1=2, so minimum coins = 2)coins.length
<= 12coins[i]
<= 2^31 - 1amount
<= 104Does the order of coins matter? No, the order does not matter.
Can the same denomination be used multiple times? Yes, you can use an unlimited number of coins of each denomination.
What should be returned if the amount
is 0?
Return 0
since no coins are needed to make an amount of 0.
Can the coins array be empty?
No, based on the constraints (1 <= coins.length
).
dp
where dp[i]
will store the minimum number of coins needed to make the amount i
.dp[0] = 0
because 0 coins are needed for amount 0.dp[i]
for i
from 1 to amount
should be set to infinity (float('inf')
), indicating that initially, it is assumed to be impossible to form that amount.c
and for each amount i
from c
to amount
, update dp[i]
with the formula:
dp[i] = min(dp[i], dp[i - c] + 1)
dp[amount]
will have our answer. If it remains infinity, return -1
.def coinChange(coins, amount):
# Initialize the dp array with infinity for all indexes except dp[0]
dp = [float('inf')] * (amount + 1)
dp[0] = 0
# Update the dp table
for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)
# If dp[amount] is still infinity, we weren't able to form 'amount'
return dp[amount] if dp[amount] != float('inf') else -1
# Example usage
print(coinChange([1, 2, 5], 11)) # Output: 3
print(coinChange([2], 3)) # Output: -1
print(coinChange([1], 0)) # Output: 0
print(coinChange([1], 2)) # Output: 2
The time complexity of the algorithm is O(n * m)
, where n
is the length of the coins
array and m
is the amount
.
dp
array which has a size of amount + 1
.This ensures that the solution is computationally feasible for the given constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?