力扣 322. 零钱兑换

1
2
3
4
5
6
7
8
def coinChange(coins, amount):
# 无限背包最大变最小问题
dp = [float("inf") for _ in range(amount + 1)]
dp[0] = 0
for i in range(len(coins)):
for j in range(coins[i], amount + 1):
dp[j] = min(dp[j], dp[j - coins[i]] + 1)
return dp[amount] if dp[amount] != float("inf") else -1
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信