作者: 负雪明烛
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. centOS6和7单用户修改密码

    CentOS6 1.       进入启动系统倒计时的时候,按esc 之后进入一下界面: 2.       按a 键进入修改内核参数页面 3.       在quiet后面加入空格和1 ,如下:回车进 ...

  2. Python os模块与sys模块

    1.os模块简单使用及说明 # -*- coding:utf-8 -*- """ os模块主要用于系统,处理程序与系统交互问题 大部分如属性等功能在linux系统中会使用 ...

  3. c#表格序号列

    <asp:BoundField HeaderText="序号" /> OnRowCreated="gridview_RowCreated" prot ...

  4. A Child's History of England.40

    Excommunication was, next to the Interdict I told you of at the close {end} of the last chapter, the ...

  5. [学习总结]5、Android的ViewGroup中事件的传递机制(二)

    下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...

  6. GO 时间处理

    比较大小 比较大小 先把当前时间格式化成相同格式的字符串,然后使用time的Before, After, Equal 方法即可. time1 := "2015-03-20 08:50:29& ...

  7. MFC入门示例之静态文本框、编辑框

    点击按钮计算文本框中文本长度 void CMFCApplication1Dlg::OnBnClickedButton1() { CString strInput; GetDlgItemText(IDC ...

  8. [MySQL实战-Mysql基础篇]-mysql架构

    1.基本组成 下面是mysql的基本架构示意图  图一 图二 我们可以从图上看出,mysql大体分为两个部分,一个是server层,另一个是引擎层. server层中包含了连接器.查询缓存.分析器.优 ...

  9. 【Matlab】CFAR/phased.CFARDetector2D

    | CFAR学习进行时ing... | CFAR原理.参数 检测阈值\(T = αP_n\) \(P_n\)是噪声功率估计,\(α\)是比例因子 训练单元:训练噪声,估计\(P_n = \frac{1 ...

  10. 『与善仁』Appium基础 — 23、操作滑动的方式

    目录 1.swipe滑动 2.scroll滑动 3.drag拖拽事件 4.滑动方法小结 5.拓展:多次滑动 6.综合练习 在Appium中提供了三种滑动的方式,swipe滑动.scroll滑动.dra ...