-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdp-12(coin change tabulation best way ).cpp
62 lines (61 loc) · 1.38 KB
/
dp-12(coin change tabulation best way ).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
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
vector<vector<int>>vec;
vector<int>x;
void fnc(int a[],int n,int m)
{
int num=1000000;
for(int i=0; i<m; i++)
{
if(n-a[i]==0) ///if coin exists in the ara just return the coin
{
x.push_back(a[i]);
return;
}
if(n-a[i]>0) ///else find out which is the optimal number
{
int sz=vec[n-a[i]].size();
num=min(num,sz);
}
}
for(int i=0; i<m; i++) /// after finding the optimal number return the solution
{
int sz=n-a[i];
if(sz>0)
{
if(vec[sz].size()==num)
{
x.push_back(a[i]);
for(int j=0; j<vec[sz].size(); j++)
{
x.push_back(vec[sz][j]);
}
break;
}
}
}
return;
}
int main()
{
int n,m;
cin>>n>>m;
int a[m];
for(int i=0; i<m; i++)cin>>a[i];
x.push_back(0);
vec.push_back(x);
for(int i=1; i<=n; i++)
{
x.clear();
fnc(a,i,3);
vec.push_back(x);
}
cout<<vec[n].size()<<endl;
for(int j=0; j<vec[n].size(); j++)
{
cout<<vec[n][j]<<" ";
}
cout<<endl;
return 0;
}