9.8 Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.

这道题给定一个钱数,让我们求用quarter,dime,nickle和penny来表示的方法总和,很明显还是要用递归来做。比如我们有50美分,那么

makeChange(50) =

  makeChange(50 using 0 quarter) +

  makeChange(50 using 1 quarter) +

  makeChange(50 using 2 quarters)

而其中第一个makeChange(50 using 0 quarter)又可以拆分为:

makeChange(50 using 0 quarter) =

  makeChange(50 using 0 quarter, 0 dimes) +

  makeChange(50 using 0 quarter, 1 dimes) +

  makeChange(50 using 0 quarter, 2 dimes) +

  makeChange(50 using 0 quarter, 3 dimes) +

  makeChange(50 using 0 quarter, 4 dimes) +

  makeChange(50 using 0 quarter, 5 dimes)

而这里面的每项又可以继续往下拆成nickle和penny,整体是一个树形结构,计算顺序是从最底层开始,也就是给定的钱数都是由penny组成的情况慢慢往回递归,加一个nickle,加两个nickle,再到加dime和quarter,参见代码如下:

解法一:.

class Solution {
public:
int makeChange(int n) {
vector<int> denoms = {, , , };
return makeChange(n, denoms, );
}
int makeChange(int amount, vector<int> denoms, int idx) {
if (idx >= denoms.size() - ) return ;
int val = denoms[idx], res = ;
for (int i = ; i * val <= amount; ++i) {
int rem = amount - i * val;
res += makeChange(rem, denoms, idx + );
}
return res;
}
};

上述代码虽然正确但是效率一般,因为存在大量的重复计算,我们可以用哈希表来保存计算过程中的结果,下次遇到相同结果时,直接从哈希表中取出来即可,参见代码如下:

解法二:

class Solution {
public:
int makeChange(int n) {
vector<int> denoms = {, , , };
vector<vector<int> > m(n + , vector<int>(denoms.size()));
return makeChange(n, denoms, , m);
}
int makeChange(int amount, vector<int> denoms, int idx, vector<vector<int> > &m) {
if (m[amount][idx] > ) return m[amount][idx];
if (idx >= denoms.size() - ) return ;
int val = denoms[idx], res = ;
for (int i = ; i * val <= amount; ++i) {
int rem = amount - i * val;
res += makeChange(rem, denoms, idx + , m);
}
m[amount][idx] = res;
return res;
}
};

[CareerCup] 9.8 Represent N Cents 美分的组成的更多相关文章

  1. [CareerCup] 9.8 Represent N Cents 组成N分钱

    9.8 Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies ...

  2. [CareerCup] 9.10 Stack Boxes 垒箱子问题

    9.10 You have a stack of n boxes, with widths w., heights hir and depths drThe boxes cannot be rotat ...

  3. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  4. [LeetCode] Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  5. Leetcode 322.零钱兑换

    零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: co ...

  6. [LeetCode] 518. Coin Change 2 硬币找零之二

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  7. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  8. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  9. CareerCup Questions List 职业杯题目列表

    网站 www.careercup.com 上的题库列表 # Title Difficulty Company 1 Guards in a museum Hard F, G  2 Bomberman H ...

随机推荐

  1. iOS UITableView删除cell分割线

    UITableView是UITableViewStylePlain风格的,这样整个TableView都会被分割线分隔开,不管有没有数据,非常丑. 为了可以自定义cell的分割线: 解决方案: 将UIT ...

  2. Microsoft Dynamics CRM 分销行业解决方案

    Microsoft Dynamics CRM 分销行业解决方案 方案亮点 360度动态渠道信息管理 充分的客户细分 全面的业务代表考核指标 业务代表管理和能力建设 业务代表过程管理 业务代表费用管理 ...

  3. 自定义母版页之列表过滤菜单位置issue fix

    问题描述: 自定义母版页,为了使左边导航和顶部导航位置不变(不滚动),将原本位于ribbon下方的#s4-workspace调整到左侧导航右边. <div id="s4-workspa ...

  4. hotCity 小程序城市选择器, 城市数据库可自己导出

    hotCity 城市选择器, 城市数据库可自己导出 后台数据API 由HotApp小程序统计提供并维护,如果需要导出并部署在公司的生产环境,最后有SQL导出下载地址 开源地址 https://gith ...

  5. Vs2013 html5开发WP8.1 APP之jquery

    仿安卓原生界面,先使用JQM,最新的JQM使用了JQ1.8.5 然后在WP8以上就会遇到动态创建控件的安全性问题 换成JQ2.X即可,但在WP8.1中一定不要删除了 <script src=&q ...

  6. Echarts xAxis boundaryGap

    Echarts  xAxis----->boundaryGap: false 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样. 类目轴中 boundaryGap 可以配置为 true 和 ...

  7. SQL SERVER错误:已超过了锁请求超时时段。 (Microsoft SQL Server,错误: 1222)

    在SSMS(Microsoft SQL Server Management Studio)里面,查看数据库对应的表的时候,会遇到"Lock Request time out period e ...

  8. 为什么忘记commit也会造成select查询的性能问题

    今天遇到一个很有意思的问题,一个开发人员反馈在测试服务器ORACLE数据库执行的一条简单SQL语句非常缓慢,他写的一个SQL没有返回任何数据,但是耗费了几分钟的时间.让我检查分析一下原因,分析解决过后 ...

  9. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path解决方案

    0.环境: win7系统,Tomcat9配置无误. 1.错误: 项目中某一.jps页面忽然出现错误,鼠标点上去为:The superclass "javax.servlet.http.Htt ...

  10. MySQL Nested-Loop Join算法学习

    不知不觉的玩了两年多的MySQL,发现很多人都说MySQL对比Oracle来说,优化器做的比较差,其实某种程度上来说确实是这样,但是毕竟MySQL才到5.7版本,Oracle都已经发展到12c了,今天 ...