8.11 Coins
2015-10-31 02:31
253 查看
For example, if we have n = 100 cents:
makeChange(100) = makeChange(100 w/ 0 quater) + makeChange(100 w/ 1 quater) + makeChange(100 w/ 2 quater) + makeChange(100 w/ 3 quater) + makeChange(100 w/ 4 quater). What’s more, makeChange(100 w/ 1 quater) = makeChange(75 w/ 0 quarter).
Under each sub problem we have makeChange(100 w/ 0 quarter) = makeChange(100 w/ 0 quarter, 1 dime to 10 dime)…
Than’s mean: we can solve the question by accumulating all there results!
However, the above code is not that optimal, because we keep recall makeChange(100 w/ …) several times. Therefore, the possible method to optimize it is by using extra space O(kn). By recording the certain amount ways, we will increase the speed. There is a basically the trade-off between memory and speed.
makeChange(100) = makeChange(100 w/ 0 quater) + makeChange(100 w/ 1 quater) + makeChange(100 w/ 2 quater) + makeChange(100 w/ 3 quater) + makeChange(100 w/ 4 quater). What’s more, makeChange(100 w/ 1 quater) = makeChange(75 w/ 0 quarter).
Under each sub problem we have makeChange(100 w/ 0 quarter) = makeChange(100 w/ 0 quarter, 1 dime to 10 dime)…
Than’s mean: we can solve the question by accumulating all there results!
//c++ int helper(vector<int>& coins, int n, int idx){ if(idx >= coins.size() - 1) return 1; int cntWays = 0; for (int i = 0; i*coins[idx] <= n; ++i) { cntWays += helper(coins, n - i*coins[idx], idx + 1); } return cntWays; } int makeChange(int n){ vector<int> coins = {25, 10, 5, 1}; return helper(coins, n, 0); }
public static int makeChangeHelper(int[] coins, int n, int idx){ if(idx >= coins.length-1) return 1; int amount = coins[idx], cntWays = 0; for(int i = 0; i*amount <= n; ++i){ cntWays += makeChangeHelper(coins, n-i*amount, idx + 1); } return cntWays; } public static int makeChange(int n, int[] coins){ return makeChangeHelper(coins, n, 0); }
However, the above code is not that optimal, because we keep recall makeChange(100 w/ …) several times. Therefore, the possible method to optimize it is by using extra space O(kn). By recording the certain amount ways, we will increase the speed. There is a basically the trade-off between memory and speed.
public static int makeChangeHelper(int[] coins, int amount, int idx, int[][] mp){ if(mp[amount][idx] > 0) return mp[amount][idx]; if(idx >= coins.length-1) return 1; int cntWays = 0; for(int i = 0; i * coins[idx] <= amount; ++i){ cntWays += makeChangeHelper(coins, amount - i*coins[idx], idx+1, mp); } mp[amount][idx] = cntWays; return cntWays; } public static int makeChange(int n, int[] coins){ int[][] mp = new int[n+1][coins.length]; return makeChangeHelper(coins,n,0,mp); }
相关文章推荐
- Yet Another Sudoku Solver in Python
- 考查嵌入式C开发人员的最好的十道题~答案(zz hecrics的BLOG)
- 代码大全学习-20-不常见的控制结构(Unusual Control Structures)
- Recursion World
- jTree 刷新问题 终极解决方法
- Nearest Neighbor Queries, Nick Roussopoulos
- SQL2005/2008中的CTE应用--递归查询
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- Unique Subsets
- Unique Permutations
- 4 Sum
- Recursion http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/recursion.html
- 初涉js的第一个问题
- caller,arguments.callee,call,apply
- Assembly in Linux (Stack, External Procedure)
- wind 4000 ows xp 上安装bind实现域名服务
- 3n+1 Problem, aka Collatz Problem
- CentOS 5.6 + ISPConfig 搭建邮件服务器全过程
- struts2 chain