-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumCoins.cpp
More file actions
52 lines (49 loc) · 989 Bytes
/
minimumCoins.cpp
File metadata and controls
52 lines (49 loc) · 989 Bytes
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
#include <iostream>
#include <climits>
#include <cstring>
using namespace std;
// Recursive
int coinsNeeded(int amount,int *d,int k){
if(amount == 0) return 0;
int ans = INT_MAX;
for(int i=0;i<k;i++){
if(amount >= d[i]){
int smallerAns = coinsNeeded(amount-d[i],d,k);
if(smallerAns != INT_MAX){
ans = min(ans,smallerAns+1);
}
}
}
return ans;
}
// BUDP
int coinsNeededBUDP(int amount,int *d,int k){
int *dp = new int[amount+1];
for(int i=0;i<=amount;i++){
dp[i] = INT_MAX;
}
dp[0] = 0;
for(int rokda = 1;rokda<=amount;rokda++){
for(int i = 0;i<k;i++){
if(rokda >= d[i]){
int smallerAns = dp[rokda-d[i]];
if(smallerAns != INT_MAX){
dp[rokda] = min(dp[rokda],smallerAns+1);
}
}
}
}
return dp[amount];
}
int main()
{
int amount,k;
cin>>amount>>k;
int d[k];
for(int i = 0;i<k;i++){
cin>>d[i];
}
// cout<<coinsNeeded(amount,d,k)<<endl;
cout<<coinsNeededBUDP(amount,d,k);
return 0;
}