Coin Change
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [float("inf")] * (amount + 1)
dp[0] = 0
for y in range(1, amount + 1):
for coin in coins:
if y - coin < 0:
continue
dp[y] = min(dp[y], dp[y - coin] + 1)
if dp[-1] == float("inf"):
return -1
return dp[-1]Last updated