【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/coin-change/description/
题目描述
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
Note:
- You may assume that you have an infinite number of each kind of coin.
题目大意
给了无限数量的面值分别为coins的硬币,问能否构成amuont。
解题方法
动态规划
题目比较重要的是硬币无限数量。我们的做法是使用动态规划,需要构建一个长度是amount + 1的dp数组,其含义是能够成面额从0到amount + 1需要使用的最少硬币数量。
所以我们对每一种面额的硬币,都去计算并更新新添了这种面额的情况下,构成的所有面额需要的最少硬币数量。注意,变量是不同面额的硬币。dp更新的策略是从coin面额到amount+1的面额依次向后查找,看这个位置能不能用更少的硬币组成(即使用面额是i - coin的需要硬币数量+1).

时间复杂度很小,但是不会算,空间复杂度是O(1).
class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for coin in coins:
for i in range(coin, amount + 1):
if dp[i - coin] != float('inf'):
dp[i] = min(dp[i], dp[i - coin] + 1)
return -1 if dp[amount] == float('inf') else dp[amount]
C++代码如下:
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
const int N = coins.size();
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int coin : coins) {
for (int i = coin; i <= amount; ++i) {
if (dp[i - coin] != INT_MAX) {
dp[i] = min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] == INT_MAX ? -1 : dp[amount];
}
};
参考资料:
https://www.youtube.com/watch?v=uUETHdijzkA
https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-322-coin-change/
日期
2018 年 10 月 31 日 —— 十月最后一天,万圣节
2019 年 1 月 15 日 —— 2019的一月过半
【LeetCode】322. Coin Change 解题报告(Python & C++)的更多相关文章
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
随机推荐
- 利用抖音Cookie充值接口提取支付链接,个人调起原生微信h5支付宝h5支付
最近开始搞一些个人支付通道的开发,方便个人不用和第三方平台签约就能收款,省去很多流程手续的成本. 然后翻了一下网上并没有太多现成的技术教程,只能自己研究着搞了. 这次要分享的是利用抖音的充值接口,去分 ...
- 突破冯·诺依曼架构瓶颈!全球首款存算一体AI芯片诞生
过去70年,计算机一直遵循冯·诺依曼架构设计,运行时数据需要在处理器和内存之间来回传输. 随着时代发展,这一工作模式面临较大挑战:在人工智能等高并发计算场景中,数据来回传输会产生巨大的功耗:目前内存系 ...
- Angular @Input讲解及用法
1.什么是@input @input的作用是定义模块输入,是用来让父级组件向子组件传递内容. 2.@input用法 首先在子组件中将需要传递给父组件的变量用@input()修饰 需要在子组件ts文件i ...
- 数组相关API,reduce
reduce() 方法接受一个数组作为输入值并返回一个值.这点挺有趣的.reduce 接受一个回调函数,回调函数参数包括一个累计器(数组每一段的累加值,它会像雪球一样增长),当前值,和索引.reduc ...
- redis安装与简单实用
1.在Linux上redis的安装时十分简单的: 第一步:wget http://download.redis.io/releases/redis-2.8.12.tar.gz 解压: tar zxvf ...
- IntentFilter,PendingIntent
1.当Intent在组件间传递时,组件如果想告知Android系统自己能够响应那些Intent,那么就需要用到IntentFilter对象. IntentFilter对象负责过滤掉组件无法响应和处理的 ...
- 【Linux卷管理】LVM创建与管理
安装LVM 首先确定系统中是否安装了lvm工具: [root@jetsen ~]# rpm -qa|grep lvm system-config-lvm-1.1.5-1.0.el5 lvm2-2.02 ...
- get请求url参数中有+、空格、=、%、&、#等特殊符号的问题解决
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...
- minkube在deban10上的安装步骤
环境准备: 所用机器为4c 16g i3 4170 1t机械硬盘 系统 debian 10 安装docker 如果已经安装并配置好可直接跳过 安装ssl sudo apt-get install ...
- C++STL标准库学习笔记(二)二分查找
二.STL中的二分查找算法 1.binary_search 2.lower_bound 3.upper_bound 记得#include<algorithm>! 前言: 在这个笔记中,我把 ...