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. jquery animate 动画效果使用解析

    animate的意思是:使有生气:驱动:使栩栩如生地动作:赋予…以生命作为形容词:有生命的:活的:有生气的:生气勃勃的 先看动画效果:http://keleyi.com/keleyi/phtml/jq ...

  2. jquery右下角自动弹出关闭层

    效果体验:http://keleyi.com/keleyi/phtml/jqtexiao/36.htm 右下角弹出层后,会在一定时间后自动隐藏.第一版本:http://www.cnblogs.com/ ...

  3. IE6/IE7中li底部4px空隙的Bug

    当li的子元素中有浮动(float)时,IE6/IE7中<li>元素的下面会产生4px空隙的bug. 代码如下: <ul class="list"> < ...

  4. 用VSCode写python的正确姿势(转载)

    最近在学习python,之前一直用notepad++作为编辑器,偶然发现了VScode便被它的颜值吸引.用过之后发现它启动快速,插件丰富,下载安装后几乎不用怎么配置就可以直接使用,而且还支持markd ...

  5. numpy中matrix的特殊属性

    一.matrix特殊属性解释 numpy中matrix有下列的特殊属性,使得矩阵计算更加容易 摘自 NumPy Reference Release 1.8.1 1.1 The N-dimensiona ...

  6. Windows on Device 项目实践 1 - PWM调光灯制作

    在前一篇文章<Wintel物联网平台-Windows IoT新手入门指南>中,我们讲解了Windows on Device硬件准备和软件开发环境的搭建,以及Hello Blinky项目的演 ...

  7. The process could not execute 'sp_repldone/sp_replcounters' on 'ServerName'

    昨天发现发布服务器S(SQL Server 2008 R2),出现大量如下错误 错误细节如下所示: Date :: PM :: PM) Source spid52 Message Replicatio ...

  8. Mysql存储过程和函数区别介绍

    存储过程是用户定义的一系列sql语句的集合,涉及特定表或其它对象的任务,用户可以调用存储过程,而函数通常是数据库已定义的方法,它接收参数并返回某种类型的值并且不涉及特定用户表. 存储过程和函数存在以下 ...

  9. (转)Div和Table的区别

    原文:http://www.cnblogs.com/lovebear/archive/2012/04/18/2456081.html Div与table的区别 1:速度和加载方式方面的区别 div 和 ...

  10. 解决undefined reference to `__poll_chk@GLIBC_2.16' 错误

    出现这个错误,是系统的glibc版本太低了,需要更新 到http://ftp.gnu.org/gnu/glibc/下载新版本的glibc,也不用太高,我选择glibc-2.20.tar.gz   解压 ...