Skip to content

Commit fe7397e

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 322_Coin_Change.java
1 parent d851d4d commit fe7397e

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution {
22
public int coinChange(int[] coins, int amount) {
33
int[] dp = new int[amount + 1];
4-
Arrays.fill(dp, amount + 1);
4+
Arrays.fill(dp, Integer.MAX_VALUE);
5+
56
dp[0] = 0;
6-
7-
for (int coin : coins) {
8-
for (int i = 1; i <= amount; i++) {
9-
if (i >= coin) {
10-
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
7+
8+
for (int sum = 1; sum <= amount; sum++) {
9+
for (int coin : coins) {
10+
if (coin <= sum && dp[sum - coin] < dp[sum]) {
11+
dp[sum] = dp[sum - coin] + 1;
1112
}
1213
}
1314
}
14-
15-
return dp[amount] == amount + 1 ? -1 : dp[amount];
15+
16+
return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
1617
}
1718
}

0 commit comments

Comments
 (0)