作者: 负雪明烛
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:

  1. 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++)的更多相关文章

  1. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  3. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  4. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  5. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  6. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  7. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  8. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  9. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

随机推荐

  1. python—模拟生成双色球号

    双色球规则:"双色球"每注投注号码由6个红色球号码和1个蓝色球号码组成.红色球号码从1--33中不重复选择:蓝色球号码从1--16中选择. # -*- coding:UTF-8 - ...

  2. Linux—yum的python版本错误——初级解决方案

    为了安装rrdtool,发现不是少这个就是少那个,最后发现yum也不能用. 从网上找的解决yum问题. 转自:http://doarthon.blog.51cto.com/3175384/728809 ...

  3. mysql 中@ 和 @@的区别

    @x 是 用户自定义的变量 (User variables are written as @var_name)@@x 是 global或session变量 (@@global @@session )@ ...

  4. 🚀 RabbitMQ课程发布-KuangStudy

    RabbitMQ课程上线(44集) 视频教程地址:https://www.kuangstudy.com/course/detail/1323452886432944129 专栏地址:https://w ...

  5. hbase参数调优

    @ 目录 HBase参数调优 hbase.regionserver.handler.count hbase.hregion.max.filesize hbase.hregion.majorcompac ...

  6. 基于 Helm 快速部署 Wordpress

    Helm 是 Kubernetes 中的一个开源软件包管理工具,Rainbond 从 5.3.1 版本开始支持部署 Helm 应用.实现 Helm 应用的便捷部署,访问控制.使 Rainbond 用户 ...

  7. 字节数与字符数mysql_mysql里一个中文汉字占多少字节数?

    在mysql中,如果是latin1字符集下,一个中文汉字占2个字节数:如果是utf8字符集下,一个中文汉字占3个字节数:如果是gbk字符集下,一个中文汉字占2个字节数. mysql各字符集下汉字和字母 ...

  8. How does “void *” differ in C and C++?

    C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not; th ...

  9. size_type 和 size_t 的区别

    标准库string里面有个函数size,用来返回字符串中的字符个数,具体用法如下:string st("The expense of spirit\n");cout << ...

  10. GCD的补充

    1-1 关于GCD中的创建和释放     在iOS6.0之前,在GCD中每当使用带creat单词的函数创建对象之后,都应该对其进行一次release操作.           在iOS6.0之后,GC ...