-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChange.cpp
More file actions
44 lines (43 loc) · 1.21 KB
/
CoinChange.cpp
File metadata and controls
44 lines (43 loc) · 1.21 KB
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
//Approach 1 :
// Dynamic programming bottom up.
// calculating the min number of coins required to make i coins i = 1 .. amount.
// moving from 1 to amount and building using previous calculated results.
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount+1, amount + 1);
dp[0] = 0;
for(int i = 1; i<=amount; i++){
for(int j = 0; j<coins.size(); j++){
if(coins[j]<=i){
dp[i] = min(dp[i], dp[i-coins[j]] + 1);
}
}
}
if(dp[amount] > amount){
return -1;
}
return dp[amount];
}
};
//Implementation 2
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount == 0){
return 0;
}
vector<long long int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for(int i = 1; i<=amount; i++){
int j = coins.size()-1 ;
while(j>=0){
if(i - coins[j] >=0){
dp[i] = min(dp[i - coins[j]] + 1, dp[i]);
}
j--;
}
}
return dp[amount] != INT_MAX ? dp[amount] : -1;
}
};