-
Notifications
You must be signed in to change notification settings - Fork 1
/
322. Coin Change.cpp
148 lines (128 loc) · 5.03 KB
/
322. Coin Change.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/************** Method-1 (BackTracking/Recursion, Gives TLE)
class Solution {
public:
int coinChangeHelper(vector<int>& coins, int amount, int start) {
// Base case
if(amount == 0) return 0;
if(start == coins.size()) return -1;
if(amount < coins[start]) return -1;
// Recursive calls
int a = coinChangeHelper(coins, amount, start+1);
int b = coinChangeHelper(coins, amount-coins[start], start);
if(a >= 0 && b >= 0) return min(a, 1+b);
if(b >= 0) return b+1;
if(a >= 0) return a;
return -1;
}
int coinChange(vector<int>& coins, int amount) {
sort(coins.begin(), coins.end());
return coinChangeHelper(coins, amount, 0);
}
};
*******************************************************************/
/******************** Method-2 (Top-Down approach, TC-O(N*amount), SC-O(N*amount)) **************
// Recursion + Memoization
class Solution {
public:
int coinChangeHelper(vector<int>& coins, int amount, int start, vector<vector<int>>& dp) {
// Base case
if(amount == 0) return 0;
if(start == coins.size()) return -1;
// Recursive calls
if(dp[start+1][amount] == -2)
dp[start+1][amount] = coinChangeHelper(coins, amount, start+1, dp);
if(amount-coins[start] >= 0 && dp[start][amount-coins[start]] == -2)
dp[start][amount-coins[start]] = coinChangeHelper(coins, amount-coins[start], start, dp);
int a = dp[start+1][amount];
int b;
if(amount-coins[start] >= 0)
b = dp[start][amount-coins[start]];
else b = -1;
if(a >= 0 && b >= 0) dp[start][amount] = min(a, 1+b);
else if(b >= 0) dp[start][amount] = b+1;
else if(a >= 0) dp[start][amount] = a;
else dp[start][amount] = -1;
return dp[start][amount];
}
int coinChange(vector<int>& coins, int amount) {
vector<vector<int>> dp(coins.size()+1, vector<int>(amount+1, -2));
dp[0][amount] = coinChangeHelper(coins, amount, 0, dp);
return dp[0][amount];
}
};
**********************************************************************************************/
/********************** Method-3 (Bottom Up, TC-O(n*amount), SC-O(n*amount)) *****************
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
vector<vector<int>> dp(n+1, vector<int>(amount+1, INT_MAX));
dp[n][0] = 0;
for(int i = n-1; i >= 0; i--) {
for(int j = 0; j <= amount; j++) {
dp[i][j] = dp[i+1][j];
if(j-coins[i] >= 0 && dp[i][j-coins[i]] != INT_MAX) {
dp[i][j] = min(dp[i][j], 1+dp[i][j-coins[i]]);
}
}
}
return dp[0][amount] == INT_MAX? -1: dp[0][amount];
}
};
**********************************************************************************************/
/********************** Method-4 (Bottom Up, TC-O(n*amount), SC-O(amount)) *****************
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
vector<vector<int>> dp(2, vector<int>(amount+1, INT_MAX));
dp[n%2][0] = 0;
for(int i = n-1; i >= 0; i--) {
for(int j = 0; j <= amount; j++) {
dp[i%2][j] = dp[(i+1)%2][j];
if(j-coins[i] >= 0 && dp[i%2][j-coins[i]] != INT_MAX) {
dp[i%2][j] = min(dp[i%2][j], 1+dp[i%2][j-coins[i]]);
}
}
}
return dp[0][amount] == INT_MAX? -1: dp[0][amount];
}
};
******************************************************************************************/
/****************** Method-5 (Bottom-Up, TC-O(n*amount), SC-O(amount)) *****************
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
// For small optimization sort in reverse order
sort(coins.begin(), coins.end(), greater<int>() );
int n = coins.size();
vector<int> dp(amount+1, INT_MAX);
// Base case
dp[0] = 0;
for(int i = 1; i <= amount; i++) {
for(int j = 0; j < n; j++) {
if(i-coins[j] >= 0 && dp[i-coins[j]] != INT_MAX)
dp[i] = min(dp[i], 1+dp[i-coins[j]]);
}
}
return dp[amount] == INT_MAX? -1: dp[amount];
}
};
*****************************************************************************************/
// Method -6 optimize approach 4
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
vector<int> dp(amount+1, INT_MAX);
dp[0] = 0;
for(int i = n-1; i >= 0; i--) {
for(int j = 0; j <= amount; j++) {
if(j-coins[i] >= 0 && dp[j-coins[i]] != INT_MAX) {
dp[j] = min(dp[j], 1+dp[j-coins[i]]);
}
}
}
return dp[amount] == INT_MAX? -1: dp[amount];
}
};