leetcode322 Coin Change
- """
- 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的更多相关文章
- leetcode322—Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- Leetcode322. Coin Change零钱兑换
给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2069 Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Coin Change (IV) (dfs)
Coin Change (IV) Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Subm ...
随机推荐
- 生成唯一的ID
public class UniqueId { public static String getUUId(){ ; int hashCodeV = UUID.randomUUID().toString ...
- Windows下MySQL5.7版本中修改编码为utf-8
我们新安装的MySQL数据库默认的字符是 latin1 ,所以每次新建数据库都要修改字符,非常麻烦.所以我们必须将它改成UTF8字符的. 修改方法如下: 一.修改MySQL的my.ini 首先在 \P ...
- 运营商如何关闭2G、3G网络?这事儿得从小灵通说起
5G时代即将全面开启,主流声音是对未来的无限畅想--5G将带来翻天覆地的变化.不过凡事都有利弊两面性,5G作为新生事物固然大有可为,但不可避免地会对旧事物造成巨大冲击.除了会影响很多跟不上潮流发展的行 ...
- javascript中slipt()分割
slipt()分割取长度 例子1: n变量其实只有两个1,结果分割成数组有3个,所以结果页取1长度的话要减去1 l 异步请求data数据输出是html,当要获取数据长度的时候, 用解析html获取长度 ...
- scp 常用命令总结
1, 上传本地文件到服务器:scp /path/local_filename username@servername:/path 例如:例如scp /var/www/test.php codingl ...
- Can you answer these queries?-HDU4027 区间开方
题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...
- 关于c++的头文件和变量声明
写再最前面:摘录于柳神的笔记: C++的头⽂件⼀般是没有像C语⾔的 .h 这样的扩展后缀的,⼀般情况下C语⾔⾥⾯的头⽂件去掉 .h 然 后在前⾯加个 c 就可以继续在C++⽂件中使⽤C语⾔头⽂件中 ...
- Java基础 -3.4
反码(~) 在计算机中,负数以其正值的补码形式表达. 什么叫补码呢?这得从原码,反码说起. 原码:一个整数,按照绝对值大小转换成的二进制数,称为原码. 比如 00000000 00000000 000 ...
- flex布局(非常重要)
首先明确一点是, flex 是 flex-grow.flex-shrink.flex-basis的缩写.故其取值可以考虑以下情况: flex 的默认值是以上三个属性值的组合.假设以上三个属性同样取默认 ...
- FF获6亿美元投资九城或许比贾跃亭更着急
互联网企业第九城市(以下简称"九城")确认,已透过旗下子公司与总部位于美国加州的法拉第未来公司签定协议,双方共同建立合资公司,在中国制造.营销及运营电动汽车.根据合资公司协议条款, ...