-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumCoins_DP.cpp
More file actions
executable file
·72 lines (53 loc) · 1.39 KB
/
MinimumCoins_DP.cpp
File metadata and controls
executable file
·72 lines (53 loc) · 1.39 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
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
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
// Top Down Approach
int minCoins_TD(int n, int coins[], int t, int dp[]){
if(n == 0){
return 0;
}
if(dp[n] != 0){
return dp[n];
}
int ans = INT_MAX;
for(int i = 0; i < t; i++){
if(n - coins[i] >= 0){
int subprob = minCoins_TD(n - coins[i], coins, t, dp);
ans = min(ans, subprob + 1);
}
}
return dp[n] = ans;
}
// Bottom Up Approach
int minCoins_BU(int n, int coins[], int t){
int dp[100] = {0};
for(int i = 1; i <= n; i++){
dp[i] = INT_MAX;
for(int j = 0; j < t; j++){
if(i-coins[j] >= 0){
int subprob = dp[i - coins[j]];
dp[i] = min(dp[i], subprob + 1);
}
}
}
return dp[n];
}
int main(){
int n;
int t;
int coins[10];
cout << "Enter the Amount" << endl;
cin >> n;
cout << "Enter the number of coins" << endl;
cin >> t;
cout << "Enter the coins" << endl;
for(int i = 0; i < t; i++){
cin >> coins[i];
}
int dp[100] = {0};
cout << "The minimum no. of coins are: " << endl;
cout << minCoins_TD(n, coins, t, dp) << endl;
cout << minCoins_BU(n, coins, t) << endl;
return 0;
}