原题

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. AVFoundation自定义录制视频

    #import <AVFoundation/AVFoundation.h> #import <AssetsLibrary/AssetsLibrary.h> @interface ...

  2. linux内核链表---挑战常规思维

    一.普通链表 1.一般教材上的链表定义如下: struct node{ int content: node *next: }: 它将指针域放在链表节点中,上一个节点指针域中的值指向下一个节点的首地址, ...

  3. Xamarin.Android Binding篇

    前言 趁着失业了,闲着没事儿学习了下Xamarin.Android binding,在以往的开发中,我相信很多人都遇到过binding的坑,也不例外, 我也踩了很多雷,好在认识了个大佬,指导了很多  ...

  4. 模拟一个shuffle

    之所以会想到写这么一个shuffle的例子,是因为一个需求:我需要把一个有序数组中的数据随机的打散.java代码如下, public void shuffle() { int[] arr = {1,2 ...

  5. 功能总结之MongDB初探

    题外话 工作3年,了解的技术颇多,但都是一知半解,了解不是很透澈.用过的技术,就像猴子搬过的包谷,搬一个丢一个.几年风雨,真有点一缕清风过,片叶不沾身的味道. 为强化知识点,提升文档及学习能力,我把以 ...

  6. echarts 各类图形小计

    通用  官网 1.提示框组件 效果: 代码: 配置项手册 2.工具框组件 效果: 代码: 配置项: 3.series系列列表 a)折线line 效果: 代码: if(serSets[v]){ //注释 ...

  7. 安全超文本传输协议(HTTPS)详解

    一.概念与摘要 HTTPS (Secure Hypertext Transfer Protocol)安全超文本传输协议,是一个安全通信通道,它基于HTTP开发用于在客户计算机和服务器之间交换信息.它使 ...

  8. Eclipse中将含有图片资源的项目打包成jar文件

    前言: 最近学了GUI编程和UDP协议,心血来潮想做一个局域网内的聊天软件,前期都还算顺利,直到后来将整个项目打包成jar文件时遇到了困难.如图: 自己设置的图标不见了,但是也没有默认的图标,说明图片 ...

  9. Zkui安装

    是一个允许在zookeeper上进行增删查改操作的图形管理工具,与zkdash类似. 1.拉取代码 #git clone https://github.com/DeemOpen/zkui.git 2. ...

  10. python爬虫实战(二)--------千图网高清图

    相关代码已经修改调试----2017-3-21 实现:千图网上高清图片的爬取 程序运行20小时,爬取大约162000张图片,一共49G,存入百度云.链接:http://pan.baidu.com/s/ ...