Skip to content

Commit 8b18aea

Browse files
Create CoinChangeII.java
1 parent 9486032 commit 8b18aea

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CoinChangeII {
2+
public int change(int amount, int[] coins) {
3+
int n = coins.length;
4+
5+
int[][] dp = new int[n][amount+1];
6+
7+
for(int i = 0; i<n; i++){
8+
dp[i][0] = 1;
9+
}
10+
11+
for(int j = 1; j<amount+1; j++){
12+
if(j != 0 && j%coins[0] == 0){
13+
dp[0][j] = 1;
14+
}
15+
}
16+
17+
for(int i = 1; i<n; i++){
18+
for(int j = 1; j<amount+1; j++){
19+
if(j>=coins[i]){
20+
dp[i][j] = dp[i-1][j] + dp[i][j-coins[i]];
21+
}
22+
else{
23+
dp[i][j] = dp[i-1][j];
24+
}
25+
}
26+
}
27+
28+
return dp[n-1][amount];
29+
}
30+
}

0 commit comments

Comments
 (0)