Leetcode 322 Coin Change
2016-05-09 10:27
337 查看
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
Example 1:
coins =
return
Example 2:
coins =
return
Note:
You may assume that you have an infinite number of each kind of coin.
基本的找零问题加上额外判断。这里先把所有元素设为amount+1,然后最后再判断。
-1.
Example 1:
coins =
[1, 2, 5], amount =
11
return
3(11 = 5 + 5 + 1)
Example 2:
coins =
[2], amount =
3
return
-1.
Note:
You may assume that you have an infinite number of each kind of coin.
基本的找零问题加上额外判断。这里先把所有元素设为amount+1,然后最后再判断。
class Solution(object): def coinChange(self, coins, amount): tot = [amount+1 for i in range(amount+1)] tot[0] = 0 for i in range(1,amount+1): for x in coins: if x <= i: tot[i] = min(tot[i-x]+1,tot[i]) if tot[-1] == amount+1: return -1 return tot[-1]
相关文章推荐
- loadrunner录制时弹出invalid application path!please check if application exists对话框
- 提交图片到svn
- 页面左右分布方案调整
- 专访|腾讯、阿里、adidas的服务方UID WORKS
- 漫游场景
- Timer定时器
- PHP面向对象的进阶学习(抽像类、接口、final、类常量)
- Android SDK版本号与API级别的对应关系以及创建工程时选择API版本的注意事项
- -XX:+PrintGCDetails 打印GC详细信息
- Java生成随机数
- select 列值操作
- 数据库用户权限
- AJAX入门
- Spring-boot构建
- sdutoj 3252 Lowest Unique Price
- 监听软键盘的显示与隐藏
- Bat命令学习 (转载)
- 简洁易用的表单数据设置和收集管理组件
- 20160509 记录点已设置
- 20160509 记录点已设置