"""
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
"""
"""
传送门:https://blog.csdn.net/qq_17550379/article/details/82909656
这实际上是一个完全背包问题,我们定义这样的方程f(amount),
我们将n个物品放入容量为amount的背包中,使得物品金额正好为amount是,所需的硬币数目最少。
我们会考虑第i个物品放入后,所需硬币数目
f(amount)=min(f(amount-coins[i])+1)
硬币1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
硬币2
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
硬币5
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]
"""
class Solution1:
def coinChange(self, coins, amount):
dp = [float('inf')]*(amount+1) #正无穷 float('inf') 负无穷 float('-inf')
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[-1] > amount else dp[-1] #如果最后解出的f(amount)>amount,那么表示无解 """
回溯法,未理解
这里我们首先将coins从大到小进行排序,这是因为我们希望硬币数量尽可能的少,
那么就需要尽量将面值大的硬币加入结果中。中间的剪枝操作也很容易理解
if coins[i] <= target < coins[i]*(result - count):
我们的目标值一定是要大于等于我们将要放入的硬币面额,而且本次使用的硬币数量一定要比上次少。
"""
class Solution2:
def coinChange(self, coins, amount):
coins.sort(reverse=True)
len_coins, result = len(coins), amount+1 def countCoins(index, target, count):
nonlocal result
if not target:
result = min(result, count) for i in range(index, len_coins):
if coins[i] <= target < coins[i]*(result - count):
countCoins(i, target - coins[i], count+1) for i in range(len_coins):
countCoins(i, amount, 0)
return -1 if result > amount else result

leetcode322 Coin Change的更多相关文章

  1. leetcode322—Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. Leetcode322. Coin Change零钱兑换

    给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = ...

  3. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  4. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  7. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  8. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

随机推荐

  1. xadmin 后台管理

    xadmin后台管理 安装:luffy虚拟环境下 >: pip install https://codeload.github.com/sshwsfc/xadmin/zip/django2 注册 ...

  2. iPhone代工商,谁敢要求苹果赔偿损失?

    据外国媒体报道,苹果的首席设计师已准备离职,有相关评论称:库克已经不在把硬件设计放到第一位,整个团队都巧妙地遭遇降级.相信熟悉苹果组织的人都知道,他们一切的核心都是围绕"硬件设计" ...

  3. 时隔两天,三星再称GalaxyFold已准备就绪,王自如的脸还好吗?

    编辑 | 禾斗 出品 | 于见(mpyujian) 据消息人士透露,三星已经完成对其有缺陷的折叠智能手机进行了重新设计,Galaxy Fold准备适时再度推出,但问题是,作为消费者,我们准备好了吗? ...

  4. jquery中for循环

    1.循环遍历标签 //定义数组 var imagesPath=[]; //循环遍历对象 $("#uploadList li img").each(function(){ image ...

  5. preg_replace相关问题

    preg_replace preg_replace 函数执行一个正则表达式的搜索和替换. 语法: preg_replace ( mixed $pattern , mixed $replacement ...

  6. 从零构建以太坊(Ethereum)智能合约到项目实战——第20章 搭建自己的私有链网络

    P75 .1-以太坊私网建立 .合约编译.部署完全教程(1) 使用此博文进行安装配置:https://blog.csdn.net/w88193363/article/details/79402074 ...

  7. python3报:ImportError: No module named 'MySQLdb'

    问题描述: 项目在转到python3.6时,原先的导入MySQLdb模块都提示无法导入,pip install mysqldb也安装失败. 问题原因: python2和python3在数据库模块支持这 ...

  8. Mate Linux 桌面的什么受GNOME 2 粉丝喜欢 ?

    导读 如果你以前听过这个传闻:当 GNOME3 第一次发布时,很多 GNOME 用户还没有准备好放弃 GNOME 2. Mate(以马黛茶yerba mate植物命名)项目的开始是为了延续 GNOME ...

  9. Linux内核5.4正式将华为EROFS超级文件系统合入主线

    导读 近期,Linux内核5.4系列宣布全面可用,添加了许多新功能,更强的安全性和更新的驱动程序,以提供更好的硬件支持.Linux内核5.4增加对微软exFAT文件系统的支持,另外还支持内核锁定功能, ...

  10. go笔记(go中的方法调用)

    最近接触go语言  发现和java的方法调用有些类似但又有自己的注意点 go的包我理解为则是隔离的最小模块 先在src目录下创建main.go文件  package为main,然后在src下创建mod ...