Skip to content

Commit 9b4bab1

Browse files
William FisetWilliam Fiset
authored andcommitted
Coin change
1 parent 58d0962 commit 9b4bab1

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed
94.9 KB
Binary file not shown.

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class CoinChange {
2121

2222
public static class Solution {
23-
// Contains the minimum number of coins to make a certain amount, if an optimal solution exists.
23+
// Contains the minimum number of coins to make a certain amount, if a solution exists.
2424
Optional<Integer> minCoins = Optional.empty();
2525

2626
// The coins selected as part of the optimal solution.
@@ -94,9 +94,12 @@ public static Solution coinChangeSpaceEfficient(int[] coins, int n) {
9494
dp[0] = 0;
9595

9696
for (int i = 1; i <= n; i++) {
97-
for (int coinValue : coins) {
98-
if (i - coinValue >= 0 && dp[i - coinValue] + 1 < dp[i]) {
99-
dp[i] = dp[i - coinValue] + 1;
97+
for (int coin : coins) {
98+
if (i - coin < 0) {
99+
continue;
100+
}
101+
if (dp[i - coin] + 1 < dp[i]) {
102+
dp[i] = dp[i - coin] + 1;
100103
}
101104
}
102105
}

0 commit comments

Comments
 (0)