Skip to content

Commit

Permalink
8.11: Removing assumption that final denomination is a penny and othe…
Browse files Browse the repository at this point in the history
…r code cleanup
  • Loading branch information
Gayle McDowell committed Nov 5, 2018
1 parent bfcf0e8 commit 7c9cf49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package Q8_11_Coins;

public class Question {
public static int makeChange(int amount, int[] denoms, int index) {
if (index >= denoms.length - 1) return 1; // one denom remaining -> one way to do it
int denomAmount = denoms[index];
public static int makeChangeHelper(int total, int[] denoms, int index) {
int coin = denoms[index];
if (index == denoms.length - 1) { // One denom left, either you can do it or not
int remaining = total % coin;
return remaining == 0 ? 1 : 0;
}
int ways = 0;
for (int i = 0; i * denomAmount <= amount; i++) {
int amountRemaining = amount - i * denomAmount;
ways += makeChange(amountRemaining, denoms, index + 1); // go to next denom
/* Try 1 coin, then 2 coins, 3 three, ... (recursing each time on rest). */
for (int amount = 0; amount <= total; amount += coin) {
ways += makeChangeHelper(total - amount, denoms, index + 1); // go to next denom
}
return ways;
}

public static int makeChange(int amount, int[] denoms) {
return makeChange(amount, denoms, 0);
return makeChangeHelper(amount, denoms, 0);
}

public static void main(String[] args) {
int[] denoms = {25, 10, 5, 1};
int ways = makeChange(300322, denoms);
int ways = makeChange(10, denoms);
System.out.println(ways);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ public class QuestionB {

public static int makeChange(int n, int[] denoms) {
int[][] map = new int[n + 1][denoms.length];
return makeChange(n, denoms, 0, map);
return makeChangeHelper(n, denoms, 0, map);
}

public static int makeChange(int amount, int[] denoms, int index, int[][] map) {
if (map[amount][index] > 0) { // retrieve value
return map[amount][index];
public static int makeChangeHelper(int total, int[] denoms, int index, int[][] map) {
/* Check cache for prior result. */
if (map[total][index] > 0) { // retrieve value
return map[total][index];
}
if (index >= denoms.length - 1) return 1; // one denom remaining -> one way to do it
int denomAmount = denoms[index];
int ways = 0;
for (int i = 0; i * denomAmount <= amount; i++) {
// go to next denom, assuming i coins of denomAmount
int amountRemaining = amount - i * denomAmount;
ways += makeChange(amountRemaining, denoms, index + 1, map);

int coin = denoms[index];
if (index == denoms.length - 1) {
int remaining = total % coin;
return remaining == 0 ? 1 : 0;
}
map[amount][index] = ways;
return ways;
int numberOfWays = 0;
/* Try 1 coin, then 2 coins, 3 three, ... (recursing each time on rest). */
for (int amount = 0; amount <= total; amount += coin) {
numberOfWays += makeChangeHelper(total - amount, denoms, index + 1, map); // go to next denom
}

/* Update cache with current result. */
map[total][index] = numberOfWays;

return numberOfWays;
}

public static void main(String[] args) {
int[] denoms = {25, 10, 5, 1};
int ways = makeChange(100000, denoms);
int ways = makeChange(10, denoms);
System.out.println(ways);
}

Expand Down

0 comments on commit 7c9cf49

Please sign in to comment.