"""
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. 炼金术(2): 为什么要用issue管理软件

    在项目开发中,存在的无数的任务分解,问题管理,流程跟踪.因为直接说话或者直接在IM里喊话是很容易的,所以在一个还没有习惯使用issue管理软件的团队中,直接说话或者直接在IM里AT,就在某些时候变成了 ...

  2. JSP页面中关于<c:if test="${...}"><c:if>标签的用法

    代码如下: <td class="showTd_HK" align="center"> <c:if test="${(rwyy01. ...

  3. 【原】简单shell练习(五)

    1.查询一个文件中某个单词出现的次数 # grep -o 'ts' ./test/txt |wc -l 2.统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l ...

  4. 根据class 属性判断所有的文本框必填

    <body> <!-- 遮罩层 --> <div id="hidediv" style="width: 100%;height: 100%; ...

  5. Linux下安装 boost 库

    1. 先去官网下载压缩包: https://www.boost.org/ 2. 解压 tar -zvxf boost_1_70_0.tar.gz 2. cd 进入根目录,然后执行: ./bootstr ...

  6. idea设置代码提示忽略大小写

  7. ThinkPHP5 动态生成图片缩略图

    需求场景 不同终端(PC端.手机端.平板),不同界面(列表页.详情页),对图片大小的要求不一样, 如果所有场景下都使用同一尺寸的图片,势必对会网络带宽及服务器性能造成一定的影响,由此需要服务器端能够根 ...

  8. 【快学springboot】SpringBoot整合Mybatis Plus

    原创声明 本文首发于头条号[Happyjava].Happy的掘金地址:https://juejin.im/user/5cc2895df265da03a630ddca,Happy的个人博客:http: ...

  9. Java基础知识笔记第十章:输入输出流

    File类 文件的属性 目录 文件的创建与删除 运行可执行文件 文件字节输入流 文件字节输出流 文件字符输入输出流 缓冲流 随机流 数组流 数据流 对象流 序列化与对象克隆 使用Scanner解析文件 ...

  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:移除默认的列表样式

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...