There was an error while loading. Please reload this page.
1 parent 8b1ce9a commit fec851dCopy full SHA for fec851d
Dynamic Programming/518_Coin_Change_2.java
@@ -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