-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathCoins.java
61 lines (56 loc) · 1.58 KB
/
Coins.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package chapter08RecursionAndDynamicProgramming;
/**
*
* Problem: 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
*/
public class Coins {
/**
* Method 1: Recursively call helper several times for the same values of
* amount and index. Duplicates
*/
public int makeChange1(int n) {
int[] denoms = { 25, 10, 5, 1 };
return helper1(n, denoms, 0);
}
public int helper1(int amount, int[] denoms, int index) {
if (index >= denoms.length - 1) {
// last denom
return 1;
}
int denomAmount = denoms[index];
int res = 0;
for (int i = 0; i * denomAmount <= amount; i++) {
int amountRemaining = amount - i * denomAmount;
res += helper1(amountRemaining, denoms, index + 1);
}
return res;
}
/**
* Method 2: Memorization. Store the previously computed values. Store a
* mapping from each pair to the precomputed result
*/
public int makeChange2(int n) {
int[] denoms = { 25, 10, 5, 1 };
int[][] map = new int[n + 1][denoms.length];
return helper2(n, denoms, 0, map);
}
public int helper2(int amount, int[] denoms, int index, int[][] map) {
if (map[amount][index] > 0) {
return map[amount][index];
}
if (index >= denoms.length - 1) {
// last denom
return 1;
}
int denomAmount = denoms[index];
int res = 0;
for (int i = 0; i * denomAmount <= amount; i++) {
int amountRemaining = amount - i * denomAmount;
res += helper1(amountRemaining, denoms, index + 1);
}
map[amount][index] = res;
return res;
}
}