Skip to content

Commit fec851d

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 518_Coin_Change_2.java
1 parent 8b1ce9a commit fec851d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int change(int amount, int[] coins) {
3+
int[][] dp = new int[coins.length + 1][amount + 1];
4+
dp[0][0] = 1;
5+
6+
for (int i = 1; i <= coins.length; i++) {
7+
dp[i][0] = 1;
8+
9+
for (int j = 1; j <= amount; j++) {
10+
int currCoinValue = coins[i - 1];
11+
12+
int withoutThisCoin = dp[i - 1][j];
13+
int withThisCoin = j >= currCoinValue ? dp[i][j - currCoinValue] : 0;
14+
15+
dp[i][j] = withThisCoin + withoutThisCoin;
16+
}
17+
}
18+
19+
return dp[coins.length][amount];
20+
}
21+
}

0 commit comments

Comments
 (0)