Skip to content

Commit bf2f0ca

Browse files
committed
Added some tests and benchmarks
1 parent 8519d96 commit bf2f0ca

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

training/RecursiveCoinChange.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import java.util.Arrays;
22

3+
/*
4+
* Given an arbitrary amount of money and a set of coin denominations,
5+
* do you know how to compute the minimum number of coins necessary
6+
* to change the informed amount of money?
7+
*
8+
* For instance, given $25 and the coins $1, $5, and $10, you can change
9+
* $25 with at least 3 coins: 10+10+5
10+
*/
311
class Main {
412

5-
/* This recursive approach solves the problem corretly
13+
/* This recursive approach solves the problem correctly
614
* but it does not scale for large inputs.
715
*
816
* Can you guess why? :)
@@ -41,23 +49,44 @@ static int faster(int value, int[] coins) {
4149
return minCoins[value];
4250
}
4351

52+
static void correctness(int testValue, int[] testCoins) {
53+
System.out.printf("Test Value: %d, Test Coins: %s\n", testValue, Arrays.toString(testCoins));
54+
int sol1 = solve(testValue, testCoins);
55+
int sol2 = faster(testValue, testCoins);
56+
if (sol1 != sol2) {
57+
System.err.printf("Wrong Answer - Recursive: %d, DP: %d\n", sol1, sol2);
58+
} else {
59+
System.out.printf("Correct Answer: %d\n", sol2);
60+
}
61+
}
62+
63+
static void efficiency(int minValue, int maxValue, int[] testCoins) {
64+
System.out.println("N\tNaive\tDP");
65+
for (int testValue = minValue; testValue <= maxValue; testValue += 10) {
66+
long t1 = System.currentTimeMillis();
67+
solve(testValue, testCoins);
68+
t1 = System.currentTimeMillis() - t1;
69+
70+
long t2 = System.currentTimeMillis();
71+
faster(testValue, testCoins);
72+
t2 = System.currentTimeMillis() - t2;
73+
74+
System.out.printf("%d\t%d\t%d\n", testValue, t1, t2);
75+
}
76+
}
77+
78+
// Some tests...
4479
public static void main(String[] args) {
45-
int value = 25;
46-
int[] coins = {5, 10, 15};
47-
System.out.println(value + " " + Arrays.toString(coins));
48-
System.out.println(solve(value, coins));
49-
System.out.println(faster(value, coins));
50-
51-
value = 40;
52-
coins = new int[]{5, 10, 25, 50};
53-
System.out.println(value + " " + Arrays.toString(coins));
54-
System.out.println(solve(value, coins));
55-
System.out.println(faster(value, coins));
56-
57-
value = 40;
58-
coins = new int[]{5, 10, 20, 25, 50};
59-
System.out.println(value + " " + Arrays.toString(coins));
60-
System.out.println(solve(value, coins));
61-
System.out.println(faster(value, coins));
80+
correctness(25, new int[]{5, 10, 15});
81+
correctness(40, new int[]{5, 10, 25, 50});
82+
correctness(40, new int[]{5, 10, 20, 25, 50});
83+
correctness(7, new int[]{1, 3, 4, 5});
84+
85+
System.out.println("----------");
86+
efficiency(100, 150, new int[]{5, 10, 20, 25, 50});
87+
88+
System.out.println("----------");
89+
int sol = faster(10_000_000, new int[]{5, 10, 20, 25, 50});
90+
System.out.printf("For N = 10^7, Coin Change = %d\n", sol);
6291
}
6392
}

0 commit comments

Comments
 (0)