-
Notifications
You must be signed in to change notification settings - Fork 5
/
CoinChange2.java
41 lines (36 loc) · 1.14 KB
/
CoinChange2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*https://leetcode.com/problems/coin-change-2/*/
class Solution {
int n;
Integer[][] table;
public int change(int amount, int[] coins)
{
n = coins.length;
table = new Integer[n+1][amount+1];
table[0][amount] = solve(coins,amount,0);
return table[0][amount];
}
private int solve(int[] coins, int amount, int index)
{
if (amount == 0) return 1;
if (table[index][amount] != null) return table[index][amount];
int result = 0;
for (int i = index; i < n; ++i)
if (coins[i] <= amount)
result += solve(coins, amount-coins[i], i);
return table[index][amount] = result;
}
}
class Solution {
public int change(int amount, int[] coins) {
int table[]=new int[amount+1];
// Base case (If given value is 0)
table[0] = 1;
// Pick all coins one by one and update the table[] values
// after the index greater than or equal to the value of the
// picked coin
for(int i=0; i<coins.length; i++)
for(int j=coins[i]; j<=amount; j++)
table[j] += table[j-coins[i]];
return table[amount];
}
}