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 ...
随机推荐
- VS2010在网络共享目录使用IntelliSense、ipch、sdf和SQL Compact Server相关问题
Microsoft SQL Compact Server 是专用于 Visual Studio 的单机SQL 数据库.数据库文件名的后缀为SDF. 而VS2010 拒绝在网络共享目录中建立和打开SDF ...
- ZooKeeper的简单理解
1 ZooKeeper的一致性特点 概念 描述 备注 顺序一致性 以ZXID来保证事务的顺序性 原子性 以ZAB保证原子操作,要么成功,要么失败 单一视图 无论客户端从哪个服务器获取到关于应用的数据都 ...
- linux CentOS6.5 安装SVN & 可视化管理工具iF.SVNAdmin
转:http://tanghenxin.lofter.com/post/1cc667b3_5ac50dc 实际系统环境: CentOS 6.5 x64 一.安装Apache 通常系统都已经装好了,但我 ...
- React中使用CSSTransitionGroup插件实现轮播图
动画效果,是一个页面上必不可少的功能,学习一个新的东西,当然就要学习,如何用新的东西,用它的方法去实现以前的东西啦.今天呢,我就在这里介绍一个试用react-addons-css-transition ...
- PMBOK 和 PRINCE2的技术不同的地方是什么
首先,PMBOK是一个框架指导,PRINCE2是一种实现方法. PMBOK是一种建议及最佳实践的集锦.PMBOK包含项目管理的工具和技术并且是一个指导,告诉我们如何做事情,在一种环境中怎样处理问题;而 ...
- python之pymysql模块学习(待完善...)
pymysql介绍 pymysql是在python3.x版本中用于连接mysql服务器的一个库.python2中则使用mysqldb. 安装方法: pip install pymysql 使用实例: ...
- 将一个对象push到数组之中的几点问题
在项目开发中我们需要向意数组中添加对象:首先想到的是利用数组的api,----push demo: var ar = [1,2,3] var ar2 = [11,22,33] var obj = { ...
- 【Electron】Electron开发入门(七):打开本地文件或者网页链接 and webview里操纵electron api
1.打开本地文件或者网页链接 // 打开系统本地文件 const {shell} = require('electron'); // Open a local file in the default ...
- iOS StoreKit
简述: 本文讲解iOS系统框架StoreKit中的SKStoreProductViewController与SKStoreReviewController这两个Controller. SKStoreP ...
- Linux命令的复习总结学习
1.-------------------------linux系统介绍------------------------------------------------------- Linux是一套 ...