Number of unique ways that ATM can tender
An atm can only dispense values of $1, $5, $20, and $50. Return the number
of unique ways that a $ amount of X can be tendered.
($1, $5) is distinct from ($5, $1)
Input: 4 Output: 1
Input: 6 Output: 3
Input: 100 Output: 954515231698
这题和Coin Change很像,只不过这题需要考虑顺序的问题。
Solution1:
用递归做。就像斐波那契数列那样。
public int atm(int x) {if (x <= 0)return x == 0 ? 1 : 0;return atm(x - 1) + atm(x - 5) + atm(x - 20) + atm(x - 100);
}
但是这样很多重复的计算,输入太大的话会栈溢出。所以我们可以考虑把中间结果保存起来。
Solution2:
public long tellMoneyCombinations(int money) {long[] f = new long[money+1];f[0] = 1;for(int i=1; i<=money; i++) {f[i] = f[i-1];if(i>=5) f[i]+=f[i-5];if(i>=20) f[i]+=f[i-20];if(i>=50) f[i]+=f[i-50];}return f[money];
}
Number of unique ways that ATM can tender相关推荐
- pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe)
pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe) ...
- mysql unique count_MySQL - Count Number of Unique Values
问题 If I have three columns: orderNumber, name, email and I would like to count how many unique email ...
- Dollar Dayz POJ - 3181(动态规划+大数高低位分离输出)
题意:就是给出二个数N,和k,有1~k种钱币,每种都 是无限个,用这些种类的钱币可以组合成总钱N有多少种方式. 解题:这就是一个完全背包,把N看成容量,钱币的类型值为 花费和价值.与记录有多少种路径的 ...
- 插图 引用 同一行两个插图_数学身份的优雅LED插图
插图 引用 同一行两个插图 by Chris Lam 克里斯·林(Chris Lam) 数学身份的优雅LED插图 (An elegant LED illustration of a mathemati ...
- UVA10338 Mischievous Children【组合数学】
Adam's parents put up a sign that says "CONGRATULATIONS". The sign is so big that exactly ...
- bzoj 1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(高精度完全背包)
1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 599 Solved: 3 ...
- Dollar Dayz-完全背包+大数处理
[题目] Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on ...
- leetcode63. Unique Paths II
题目要求 Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. H ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
最新文章
- Crystal Reports中的字段
- JIPB:白洋组综述根系微生物组群落构建及其对植物适应性的贡献
- python学起来难吗-Python入门学习难吗,新手如何学习
- 【Flutter】Icons 组件 ( 加载 Flutter 内置的图标 | 材料设计图标完整展示 )
- 邀请函|WorkShop报名通道开启,来就送礼!
- intellij IDEA:Error : java 不支持发行版本xxx 的问题
- 吃冻梨对人会有什么好处?
- 从入门到入土:[SEED-Lab]MD5碰撞试验|MD5collgen实验|linux|Ubuntu|MD5 Collision Attack Lab|详细讲解
- AutoPlay Menu Builder入门教程
- php留言板开发功能,PHP实现基本留言板功能原理与步骤详解
- 【VScode】ubuntu系统的 VScode 调不出中文输入法
- 前端开发者必会的英语单词
- 论文阅读 (70):Exploring Self-attention for Image Recognition
- 直流电机开发笔记3—PI调节器的数字实现(代码/仿真)
- 帝都地铁隧道里的动态广告是什么原理?
- Springboot配置多个数据源
- iPhone手机使用:手机上面的App Store突然变成英文(iTunes也是英文),然后把英文还原成中文的方法
- kotlin类的成员变量 方法
- linux根据修改日期删除,在Linux下怎么一次性删除掉那些修改时间为指定日期的所有文件?...
- 24C01SC芯片介绍