forked from super30admin/DP-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinChain2.java
83 lines (56 loc) · 1.88 KB
/
CoinChain2.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Brute Force :
class Solution {
public int change(int amount, int[] coins) {
if(coins.length == 0) return 0;
return helper(coins,0,amount);
}
private int helper(int[] coins, int idx, int amount){
//base
if(amount == 0) return 1;
if(idx==coins.length || amount < 0) return 0;
//choose
int case1 = helper(coins, idx, amount-coins[idx]);
//notchoose
int case2 = helper(coins, idx+1, amount);
return case1+case2;
}
}
/**
*
* Here we have to find all possible coins that are forming the amount. So going in an exhaustive way.
Why does an exhaustive solution have TC - exponential (2^n)? → bcz every node in a tree has 2 choices (choose, notchoose).
*
*
*/
class Solution {
public int change(int amount, int[] coins) {
if (coins == null || coins.length == 0) {
return amount == 0 ? 1 : 0;
}
int n = amount;
int m = coins.length;
int[][] dp = new int[m + 1][n + 1];
dp[0][0] = 1;
for (int i = 1; i <= m; i++) {
dp[i][0] = 1;
}
for(int i=1; i<=m; i++){
for(int j=1; j<=n;j++){
if(j < coins[i-1]){
dp[i][j] = dp[i-1][j];
}
else{
dp[i][j] = dp[i-1][j]+dp[i][j-coins[i-1]];
}
}
}
return dp[m][n];
}
}
/**
TC : O(m*n)
SC : O(m*n)
Description : Creating a 2D matrix, in the 0th index initally adding 1 bcz we have to return number opf ways.
comparing "j<Coins[i-1]", if j is less than perticular coin, we can put same value as "dp[i][j] = dp[i-1][j]"
otherwise adding above element "dp[i-1][j]" with amount back with current coin "dp[i-1][j]"
*/