Leetcode 322. 零钱兑换 (每日一题 20210824)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 322. 零钱兑换 (每日一题 20210824)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給你一個整數數組 coins ,表示不同面額的硬幣;以及一個整數 amount ,表示總金額。計算并返回可以湊成總金額所需的 最少的硬幣個數 。如果沒有任何一種硬幣組合能組成總金額,返回?-1 。你可以認為每種硬幣的數量是無限的。示例?1:輸入:coins = [1, 2, 5], amount = 11
輸出:3
解釋:11 = 5 + 5 + 1
示例 2:輸入:coins = [2], amount = 3
輸出:-1
示例 3:輸入:coins = [1], amount = 0
輸出:0
示例 4:輸入:coins = [1], amount = 1
輸出:1
示例 5:輸入:coins = [1], amount = 2
輸出:2鏈接:https://leetcode-cn.com/problems/coin-changeclass Solution:def coinChange(self, coins:List[int], amount:int)->int:dp = [0] + float('inf') * amountfor coin in coins:for x in range(coin, amount):dp[x] = min(dp[x], dp[x-coin] + 1)return -1 if dp[-1] == float('inf') else dp[-1]
總結
以上是生活随笔為你收集整理的Leetcode 322. 零钱兑换 (每日一题 20210824)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode 剑指 Offer 24
- 下一篇: Leetcode 剑指 Offer 40