-
Notifications
You must be signed in to change notification settings - Fork 18
/
code_TD(Minimizing Coins).cpp
57 lines (41 loc) · 1.1 KB
/
code_TD(Minimizing Coins).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
//Here, We use the Top Down DP approach to solve the question.
// Note that this code gives TLE. Uploading it just for understanding purpose.
#include <bits/stdc++.h>
using namespace std;
int fun(long long int x,vector<long long int> coins,long long int n,vector<long long int> dp)
{
//Base case
if(x==0)
return 0;
//Lookup
if(dp[x] != 0)
return dp[x];
//Rec case
long long int ans=INT_MAX;
for(long long int i=0;i<n;i++)
{
if(x-coins[i] >= 0)
{
long long int s = fun(x-coins[i],coins,n,dp);
ans = min(ans,s+1);
}
}
return dp[x] = ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int c,n,x;
cin>>n>>x;
vector<long long int> coins;
for(int i=0;i<n;i++)
{
cin>>c;
coins.push_back(c);
}
vector<long long int> dp(x+1,0);
cout << (fun(x,coins,n,dp) >= INT_MAX ? -1 : fun(x,coins,n,dp)) << endl;
return 0;
}