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.
solution
Use dynamic programming to keep track of the minimum number of coins required to make each amount from 0 to amount.
Let be the value of the dp array at i. Let be the set of coin values, and be the value of the coin at index in the coins array. Then, our recursive relation is as follows:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [0]*(amount+1)
for i in range(1, amount+1):
m = float("inf")
for coin in coins:
if i-coin >= 0 and dp[i-coin] >= 0:
m = min(m, dp[i-coin]+1)
dp[i] = m
return dp[-1] if dp[-1] != float("inf") else -1