【LeetCode】518. Coin Change 2 解题报告(Python)

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/coin-change-2/description/

题目描述:

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

Note: You can assume that

  1. 0 <= amount <= 5000
  2. 1 <= coin <= 5000
  3. the number of coins is less than 500
  4. the answer is guaranteed to fit into signed 32-bit integer

Example 1:

Input: amount = 5, coins = [1, 2, 5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.

Example 3:

Input: amount = 10, coins = [10]
Output: 1

题目大意

有一堆一定面额的硬币,问有多少种可以组成amount的方案。假定硬币的数量是不限量的。

解题方法

DP。第一感觉是完全背包问题,但其实由于没有重量和价值的对应关系,所以不一样。

生成了一个一维数组dp,dp[i]代表了生成总价值为i有多少方案。

对已有的所有面值的硬币进行遍历,其实思路很简单:dp[i] += dp[i - coin],价值为i的解决方案应该加上价值为i - coin的解决方案。

时间复杂度是O(L * A),空间复杂度是O(A); A = amount.

代码如下:

class Solution(object):
def change(self, amount, coins):
"""
:type amount: int
:type coins: List[int]
:rtype: int
"""
dp = [0] * (amount + 1)
dp[0] = 1
for coin in coins:
for i in range(1, amount + 1):
if coin <= i:
dp[i] += dp[i - coin]
return dp[amount]

参考资料:

https://www.youtube.com/watch?v=jaNZ83Q3QGc

日期

2018 年 9 月 25 日 —— 美好的一周又开始了,划重点,今天是周二

【LeetCode】518. Coin Change 2 解题报告(Python)的更多相关文章

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

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

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

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

  3. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  4. 【LeetCode】649. Dota2 Senate 解题报告(Python)

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

  5. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  6. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  7. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  8. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

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

随机推荐

  1. vim 的使用

    基本操作:  命令行模式 进入命令行 打开文本的时候,直接进去命令行模式 在其它模式按ESC,可以进入命令行模式 新建进入了命令行模式 光标进入末行"G"(shift+按键g,自学 ...

  2. Java的那些小事

    一,JDK和JRE有什么区别? JRE:Java Runtime Environment(java运行时环境).即java程序的运行时环境,包含了java虚拟机,java基础类库. JDK:Java ...

  3. Hadoop入门 完全分布式运行模式-准备

    目录 Hadoop运行环境 完全分布式运行模式(重点) scp secure copy 安全拷贝 1 hadoop102上的JDK文件推给103 2 hadoop103从102上拉取Hadoop文件 ...

  4. Qt最好用评价最高的是哪个版本?

    来源: http://www.qtcn.org/bbs/read-htm-tid-89455.html /// Qt4:    4.8.7      4.X 系列终结版本 Qt5 :   5.6 LT ...

  5. Hadoop 相关知识点(二)

    1.HDFS副本机制 Hadoopde 默认副本布局策略是: (1)在运行客户端的节点上放置第一个副本(如果客户端运行在集群之外,就随机选择一个节点,不过系统会避免选择那些存储太满或者太忙的节点): ...

  6. python下载openpyxl

    直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...

  7. Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)

    从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...

  8. tomcat启动和停止脚本

    #!/bin/bash JDK_HOME=/apps/jdk1.7.0_79 CATALINA_HOME=/apps/tomcat export JDK_HOME CATALINA_HOME sour ...

  9. Linux学习 - 文件系统属性chattr权限

    change file attributes on 啊linux file system 1 功能 可以防止误操作 2 chattr命令格式 chattr [+-=] [选项] 文件或目录名 + 增加 ...

  10. 编译工具sbt部署

    目录 一.简介 二.部署 三.测试 一.简介 项目构建工具是项目开发中非常重要的一个部分,充分利用好它能够极大的提高项目开发的效率.在学习SCALA的过程中,我遇到了SBT(Simple Build ...