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 ...
随机推荐
- C++判断txt文件编码格式
转载:https://blog.csdn.net/kikityan/article/details/89923808 记事本打开txt文件,然后另存,有四种编码格式可供选择,分别是:ANSI ...
- Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[100007];vec ...
- mcast_unblock_source函数
#include <errno.h> #include <sys/socket.h> #define SA struct sockaddr int mcast_unblock_ ...
- DoublyLinkedList(双向链表)
本来还不会写双向链表的,但最近学习了二叉树后,突然意识到这不就是双向链表嘛,然后通过对二叉树的理解,实现了一下双向链表. 代码: #define SIZE 10 DouLL * head, *n, * ...
- SpringMVC 入门demo
(1)新建Spring项目 (2)添加所需要的jar包 spring的5+2: spring-core.jar spring.beans.jar spring-context.jar spring-e ...
- JavaScript - Promise对象
优点: 在异步执行的流程中,把执行代码和处理结果的代码清晰地分离(因为Promise对象有链式写法,有then和catch) 组合使用Promise,就可以把很多异步任务以并行和串行的方式组合起来执行 ...
- linux 从一台机器复制文件到另一台linux机器上去
1.功能说明scp就是security copy,用于将文件或者目录从一个Linux系统拷贝到另一个Linux系统下.scp传输数据用的是SSH协议,保证了数据传输的安全,其格式如下:scp 远程用户 ...
- Plastic Sprayer Manufacturer - How Does The Sprayer Work?
The Plastic Sprayers Manufacturer stated that the sprayer is a very useful type of machine and a g ...
- 3 HTML标题&元素&图像&属性&字体增强&链接&头部标签与元素
HTML标题(heading) 通过<h1>~~~<h6>定义,每个元素代表文档中不同级别的内容. h1表示主标题,the main heading , h2,3分别表示二级. ...
- 解决HTML5(富文本内容)连续数字、字母不自动换行
最近开发了一个与富文本相关的功能,大概描述一下:通过富文本编辑器添加的内容,通过input展示出来(这里用到了 Vue 的 v-html 指令). 也是巧合,编辑了一个只有数字组成的长文本,等到展示的 ...