|
1 | 1 | import java.util.Arrays; |
2 | 2 |
|
| 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 | + */ |
3 | 11 | class Main { |
4 | 12 |
|
5 | | - /* This recursive approach solves the problem corretly |
| 13 | + /* This recursive approach solves the problem correctly |
6 | 14 | * but it does not scale for large inputs. |
7 | 15 | * |
8 | 16 | * Can you guess why? :) |
@@ -41,23 +49,44 @@ static int faster(int value, int[] coins) { |
41 | 49 | return minCoins[value]; |
42 | 50 | } |
43 | 51 |
|
| 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... |
44 | 79 | 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); |
62 | 91 | } |
63 | 92 | } |
0 commit comments