Skip to content

Commit 91f3a78

Browse files
authored
Merge pull request neetcode-gh#636 from Rixant/518-coin-change-2
java: Created coin change ii
2 parents 5161ef7 + 52452bc commit 91f3a78

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

java/518-Coin-Change-2.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Dynammic Programming - Tabulation
2+
// Time Complexity (n * amount) | Space Complexity (amount) where n is the length of coins
3+
class Solution {
4+
public int change(int amount, int[] coins) {
5+
6+
int n = coins.length;
7+
int[] dp = new int[amount + 1];
8+
Arrays.fill(dp, 0);
9+
10+
// if amount is 0, there is only 1 way of making change (no money)
11+
dp[0] = 1;
12+
13+
for (int coin: coins){
14+
for (int i = 1; i <= amount; i++){
15+
if (coin <= i){
16+
dp[i] = dp[i] + dp[i - coin];
17+
}
18+
}
19+
}
20+
21+
return dp[amount];
22+
23+
}
24+
}

0 commit comments

Comments
 (0)