Skip to content

Commit bea536f

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Coin change slides
1 parent e89dbe6 commit bea536f

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed
76.1 KB
Binary file not shown.

src/main/java/com/williamfiset/algorithms/dp/CoinChange.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*
66
* <p>Tested against: https://leetcode.com/problems/coin-change
77
*
8-
* Run locally:
9-
*
10-
* ./gradlew run -Palgorithm=dp.CoinChange
11-
*
8+
* <p>Run locally:
9+
*
10+
* <p>./gradlew run -Palgorithm=dp.CoinChange
11+
*
1212
* @author William Fiset, [email protected]
1313
*/
1414
package com.williamfiset.algorithms.dp;
@@ -66,7 +66,7 @@ public static Solution coinChange(int[] coins, final int n) {
6666
}
6767
}
6868

69-
// p(dp);
69+
p(dp);
7070

7171
Solution solution = new Solution();
7272

@@ -78,7 +78,7 @@ public static Solution coinChange(int[] coins, final int n) {
7878
}
7979

8080
for (int change = n, coinIndex = m; coinIndex > 0; ) {
81-
int coinValue = coins[coinIndex-1];
81+
int coinValue = coins[coinIndex - 1];
8282
boolean canSelectCoin = change - coinValue >= 0;
8383
if (canSelectCoin && dp[coinIndex][change - coinValue] < dp[coinIndex][change]) {
8484
solution.selectedCoins.add(coinValue);
@@ -145,7 +145,17 @@ private static int coinChangeRecursive(int n, int[] coins, int[] dp) {
145145
public static void main(String[] args) {
146146
// example1();
147147
// example2();
148-
example3();
148+
// example3();
149+
example4();
150+
}
151+
152+
private static void example4() {
153+
int n = 11;
154+
int[] coins = {2, 4, 1};
155+
System.out.println(coinChange(coins, n).minCoins);
156+
System.out.println(coinChangeSpaceEfficient(coins, n));
157+
System.out.println(coinChangeRecursive(coins, n));
158+
System.out.println(coinChange(coins, n).selectedCoins);
149159
}
150160

151161
private static void example1() {

src/test/java/com/williamfiset/algorithms/dp/CoinChangeTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void testCoinChangeSelectedCoins() {
4343
selectedCoinsSum += v;
4444
}
4545
if (solution.minCoins == -1) {
46-
assertThat(solution.selectedCoins.size()).isEqualTo(0);
46+
assertThat(solution.selectedCoins.size()).isEqualTo(0);
4747
} else {
4848
// Verify that the size of the selected coins is equal to the optimal solution.
4949
assertThat(solution.selectedCoins.size()).isEqualTo(solution.minCoins);

0 commit comments

Comments
 (0)