标签:coin coins 322 amount Change Coin LeetCode dp
322. Coin Change
Description:
You are given coins of different denominations and a total amount of money amount. 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
.
Example 1:
Input: coins = [1, 2, 5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
Example 2:
Input: coins = [2], amount = 3
Output: -1
Note:
You may assume that you have an infinite number of each kind of coin.
解题思路:
Python已经AC的代码:
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1 for _ in range(amount + 1)]
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
dp[i] = min(dp[i], dp[i - coin] + 1)
return -1 if dp[amount] >= amount + 1 else dp[amount]
Reference:
【1】花花酱 LeetCode 322. Coin Change - 刷题找工作 EP148,地址:https://www.bilibili.com/video/av31621107?from=search&seid=12222304351393275021
Microstrong0305 博客专家 发布了285 篇原创文章 · 获赞 892 · 访问量 111万+ 关注标签:coin,coins,322,amount,Change,Coin,LeetCode,dp 来源: https://blog.csdn.net/program_developer/article/details/104090349
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。