原题

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.

Note:
You may assume that you have an infinite number of each kind of coin.

示例

Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)

Example 2:
coins = [2], amount = 3
return -1.

思路

比较典型的动态规划题目。要确定每个amount最少需要多少硬币,可以用amount依次减去每个硬币的面值,查看剩下总额最少需要多少硬币,取其中最小的加一即是当前amount需要的最少硬币数,这样就得到了递推公式,题目就迎刃而解了。

代码实现

# 动态规划
class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
# 边界条件
if amount == 0:
return 0
# 存储之前计算过的结果
dp = [sys.maxint] * (amount + 1)
dp[0] = 0
# 自底向下编写递推式
for i in xrange(1,amount+1):
for j in xrange(len(coins)):
if (i >= coins[j] and dp[i - coins[j]] != sys.maxint):
# 当前数额的最小步数
dp[i] = min(dp[i], dp[i - coins[j]] + 1) # 如果最小步数等于最大值,则代表无解
return -1 if dp[amount] == sys.maxint else dp[amount]

  

LeetCode 322. Coin Change的更多相关文章

  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] 518. Coin Change 2 硬币找零 2

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

  4. LeetCode OJ 322. Coin Change DP求解

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

  5. [LeetCode] 518. Coin Change 2 硬币找零之二

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

  6. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 【Leetcode】322. Coin Change

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

  8. leetcode:Coin Change

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

  9. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

随机推荐

  1. 动态规划略有所得 数字三角形(POJ1163)

    在上面的数字三角形中寻找一条从顶部到底边的路径,使得路径上所经过的数字之和最大.路径上的每一步都只能往左下或 右下走.只需要求出这个最大和即可,不必给出具体路径. 三角形的行数大于1小于等于100,数 ...

  2. ubuntu svn下载代码出错

    ubuntu svn下载代码出错: svn: OPTIONS of 'https://server.domain.local/svn/repo': SSL handshake failed: SSL ...

  3. Web 页面测试总结—控件类

    web端页面测试,最常见的是基本控件的测试,只有了解常见的控件和其测试方法,才能掌握测试要点,避免漏测情况发生.根据日常工作总结,将控件和常见逻辑集合在一起,总结了几个控件类测试查场景如下. 导航条 ...

  4. 关于web测试收集

    页面部分 页面清单是否完整(是否已经将所需要的页面全部都列出来了) 页面是否显示(在不同分辨率下页面是否存在,在不同浏览器版本中页面是是否显示) 页面在窗口中的显示是否正确.美观(在调整浏览器窗口大小 ...

  5. cuda编程学习4——Julia

    书上的例子编译会有错误,修改一下行即可. __device__ cuComplex(float a,float b):r(a),i(b){} /* ========================== ...

  6. Java单例模式的各种实现(饿汉、懒汉、静态内部类、static代码块、enum枚举类型)

    饿汉模式 饿汉模式就是立即加载,在方法调用前,实例就已经被创建了,所以是线程安全的. public class MyObject1 { private static MyObject1 myObjec ...

  7. Unity3D动态读取外部MP3文件给AudioSource

    在PC端VR游戏开发中,需要动态加载本地的MP3文件,但是Unity3D不知道出于什么原因,到5.4.0也不支持MP3文件的外部加载(目前只支持wav和ogg). 因此要想通过www来加载mp3文件就 ...

  8. Unity3D Layer要点

    简介         Layer可以用于光照的分层和物理碰撞的分层,这样可以很好地进行性能优化 数据结构         Layer在Unity中有3中呈现方式:1.string名字,2.int层索引 ...

  9. 解决vagrant up启动失败,停留在Booting VM...过程的方法

    如图,这种情况是因为没有正确关闭虚拟机导致的,关闭时应在命令行使用vagrant halt 命令,如果直接关机便会出现这种情况. 解决办法:直接启动VirtualBox,并在命令行vagrant ha ...

  10. Linux常用命令整理

    1.常用命令:cd 进入 ls(list)查看当前目录下的文件 pwd 查看目录的路径 who an i 查看当前用户 clear 清除屏幕   2.绝对路径:从根目录开始\ 相对路径:上一层.下一层 ...