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 ...
 
随机推荐
- iOS程序进入后台,延迟指定时间退出
			
程序进入后台,延迟指定时间退出 正常程序退出后,会在几秒内停止工作:要想申请更长的时间,需要用到beginBackgroundTaskWithExpirationHandlerendBackgroun ...
 - python 接口自动化测试--代码实现(八)
			
用例读入数据库: #! /usr/bin/python # coding:utf-8 import sys,os from Engine import DataEngine reload(sys) s ...
 - sqlplus入门使用
			
1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执行 2.确保路径存在,比如[D:\oracle\oradata\Oracle9i\]也就是你要保存文件的路径存在 /*分 ...
 - RobotFramework中解析中文报错UnicodeDecodeError
			
在RobotFramework中解析一段包含中文的字符串时遇到下面的报错: FAIL : UnicodeDecodeError: 'ascii' codec can't decode byte 0xe ...
 - Linux关机重启指令
			
关机: init 0 [使用Linux的运行级别] halt poweroff shutdown -h [系统会发出广播信息,显示即将关机时间] shutdown -c [取消关机计划] 重启: r ...
 - C语言sqrt函数
			
引入头文件: # include <math.h> sqrt用来求给定值的平方根 double sqrt (double x) 在使用GCC编译时 加入 -lm参数
 - 树链剖分-SPOJ375(QTREE)
			
QTREE - Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, a ...
 - 知问前端——html+jq+jq_ui+ajax
			
**************************************************************************************************** ...
 - 会话管理(Cookie/Session技术)
			
什么是会话:用户打开浏览器,点击多个超链接,访问服务器的多个web资源,然后关闭浏览器,整个过程就称为一个会话: 会话过程需要解决的问题:每个用户在使用浏览器与服务器进行会话的过程中,都可能会产生一些 ...
 - js原生API----查找dom
			
一.祖先接口Node,及他的扩展接口EventTarget Node是一个接口,许多DOM类型从这个接口继承,并允许类似地处理(或测试)这些各种类型. 以下接口都从Node继承其方法和属性: Docu ...