MCPcopy Create free account
hub / github.com/careercup/ctci / makeChange

Method makeChange

java/Chapter 9/Question9_8/Question.java:6–15  ·  view source on GitHub ↗
(int amount, int[] denoms, int index)

Source from the content-addressed store, hash-verified

4
5public class Question {
6 public static int makeChange(int amount, int[] denoms, int index) {
7 if (index >= denoms.length - 1) return 1; // one denom remaining -> one way to do it
8 int denomAmount = denoms[index];
9 int ways = 0;
10 for (int i = 0; i * denomAmount <= amount; i++) {
11 int amountRemaining = amount - i * denomAmount;
12 ways += makeChange(amountRemaining, denoms, index + 1); // go to next denom
13 }
14 return ways;
15 }
16
17 public static int makeChange1(int n) {
18 int[] denoms = {25, 10, 5, 1};

Callers 2

makeChange1Method · 0.95
mainMethod · 0.95

Calls 2

makeChange1Method · 0.95
makeChange2Method · 0.95

Tested by

no test coverage detected