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 ...
随机推荐
- 杭电 2096 小明A+B
小明A+B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Java面向对象编程 -1.5
对象引用传递分析 类本身属于引用传递类型,既然是引用传递类型,那么就牵扯到内存的引用传递 所谓的引用传递的本质:同一块堆内存空间可以被不同的栈内存所指向,也可以更换指向. class Person{ ...
- 子元素使用position:fixed,导致他的宽度不能和父元素保持一致的解决方案
最近在编码过程中,遇到过这样一个问题,代码如下,我们有一个父级,他有一定的宽度,在他的里面有两个子级,其中一个是绝对定位的,且要求他们的宽度都和父级保持一致,然后问题就出现了,我们会发现,有了定位的s ...
- Linux--如何通过图形界面选项快速更改ubuntu的窗口、图标、分辨率大小,超详细超实用~(适合所有人群)
这是默认ubuntu给出的分辨率大小:800*600,说实话有点小不适合操作. (分辨率800*600) 这是调整后的分辨率大小. (分辨率1400*900) 操作方法: 首先点击选项框中的设置图标- ...
- oracle 高级函数
原 oracle 高级函数 2017年08月17日 16:44:19 阅读数:1731 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013278 ...
- 【协作式原创】查漏补缺之Golang中mutex源码实现(预备知识)
预备知识 CAS机制 1. 是什么 参考附录3 CAS 是项乐观锁技术,当多个线程尝试使用 CAS 同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是 ...
- Oracle忘记密码怎么办?
1.打开cmd,输入sqlplus /nolog,回车:输入“conn / as sysdba”;输入“alter user sys identified by 新密码”,注意:新密码最好以字母开头, ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:标题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:让表格更加紧凑
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- java并发初探ConcurrentSkipListMap
java并发初探ConcurrentSkipListMap ConcurrentSkipListMap以调表这种数据结构以空间换时间获得效率,通过volatile和CAS操作保证线程安全,而且它保证了 ...