"""
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. node express 应用笔记关键点

    1.处理客户端 POST数据(标签 <form>), req.body 对象 app.js var bodyParser = require('body-parser'); ... ... ...

  2. JS vue 组件创建过程

    https://www.jianshu.com/p/3504a1edba42 vue.js原生组件化开发(一)——组件开发基础 0.3472017.05.09 12:00:54字数 1120阅读 33 ...

  3. security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String ";"

    今天有个接口打算使用矩阵变量来绑定参数,即使用@MatrixVariable注解来接收参数 调用接口后项目报了如下错误 org.springframework.security.web.firewal ...

  4. 【协作式原创】查漏补缺之Go并发问题(单核多核)

    主要回答一下几个问题 1.单核并发问题 2.多核并发问题 2.几个不正确的同步案例 1.单核并发问题 先看一段go(1.11)代码: 单核CPU,1万个携程,每个携程执行100次+1操作, 思考n最终 ...

  5. 什么是 SDK?

    通俗而言: 1.其实很简单,SDK 就是 Software Development Kit 的缩写,中问意思是: 软件开发工具包. 2.这是一个覆盖面相当广泛的名词,可以这么说: 辅助开发某一类软件的 ...

  6. VIM学习笔记一

    键位图 转自:链接 永久显示行号: vi ~/.vimrc 加入 :set number 命令 简单说明 :w 保存编辑后的文件内容,但不退出vim编辑器.这个命令的作用是把内存缓冲区中的数据写到启动 ...

  7. Caffe2 模型与数据集(Models and Datasets)[3]

    Models and Datasets 这一节没什么有用的信息为了保证教程完整性,这里仍然保留这一节. 这一节唯一提到的一点就是: Caffe2的模型文件后缀是:.pb2 结语: 转载请注明出处:ht ...

  8. 凤凰系统(Phoenix OS)PC版安装,电脑上体验功能丰富的安卓系统

    PC版(X86版)ISO镜像下载地址:http://www.phoenixos.com/download_x86 下载完成后,可按照官方给出的安装教程进行安装. 凤凰系统帮助中心:http://www ...

  9. tensorflow变量的使用(02-2)

    import tensorflow as tf x=tf.Variable([1,2]) a=tf.constant([3,3]) sub=tf.subtract(x,a) #增加一个减法op add ...

  10. cookie、 Session Storage 、 Local Storage

    问题描述: 使用Ajax, Controller 传回来 JSON 字符串(待处理的信息) 在 Ajax 中实现页面跳转 window.location.href="/jsp/index.j ...