LeetCode 322. Coin Change
原题
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
return3
(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的更多相关文章
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【Leetcode】322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode:Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- 322. Coin Change
动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...
随机推荐
- 求int型正整数在内存中存储时1的个数
题目描述: 输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数. 输入描述: 输入一个整数(int类型) 输出描述: 这个数转换成2进制后,输出1的个数 输入例子: 5 输出例子: ...
- 3404: [Usaco2009 Open]Cow Digit Game又见数字游戏
3404: [Usaco2009 Open]Cow Digit Game又见数字游戏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 72 Solved ...
- Golang版protobuf编译
官方网址: https://developers.google.com/protocol-buffers/ (需要FQ) 代码仓库: https://github.com/google/protobu ...
- HTML5学习笔记<四>: 列表, 块和布局
HTML列表 列表标签 标签 描述 <ol> 定义有序列表. <ul> 定义无序列表. <li> 定义列表项. <dl> 定义定义列表. <dt& ...
- HTML——超文本标记语言(表单及12个表单元素)
表单 格式: <form action=" " method="get/post" placehoder=" "></f ...
- jsp中九大内置对象
jsp实质是一个Servlet类,当jsp页面第一次被访问时,就会被服务器翻译成.java文件,紧接着就编译成.class文件. jsp<% %>和<%= %>脚本中可以直接使 ...
- 当git上文件大小写重命名的修改时(git大小写敏感/默认不敏感),如何提交
git默认是大小写不敏感!!! 加了感叹号是什么意思呢,意思就是这本身就是一个坑,本人使用的IDE是idea(网上说Eclipse可以避开问题),这个IDE本身就集成了git,但是如果要在termin ...
- 基于模糊聚类和最小割的层次化网格分割算法(Hierarchical Mesh Decomposition)
网格分割算法是三维几何处理算法中的重要算法,具有许多实际应用.[Katz et al. 2003]提出了一种新型的层次化网格分割算法,该算法能够将几何模型沿着凹形区域分割成不同的几何部分,并且可以避免 ...
- iOS 10 语音识别Speech Framework详解
最近做了一个项目,涉及到语音识别,使用的是iOS的speech Framework框架,在网上搜了很多资料,也看了很多博客,但介绍的不是很详细,正好项目做完,在这里给大家详解一下speech Fram ...
- line-height属性总结
line-height属性的继承性: 子元素不设置line-height时, 在父元上设置带单位的值和百分比时会先计算父元素的line-height大小然后继承过来,在父元素上设置无单位的数值时,子 ...