-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change.cpp
More file actions
56 lines (48 loc) · 1.09 KB
/
coin_change.cpp
File metadata and controls
56 lines (48 loc) · 1.09 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
#include<bits/stdc++.h>
using namespace std;
// Given a value N, if we want to make change for N cents,
// and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins,
// how many ways can we make the change? The order of coins doesn’t matter.
int coin_change(int N, vector<int> S, int m)
{
if (N==0) return 1;
if (N<0) return 0;
if (m<=0 && N>=1) return 0;
return coin_change(N-S[m-1],S,m)+coin_change(N,S,m-1);
}
int coin_change_DP(int N, vector<int> S, int m)
{
int DP[N+1][m+1];
for(int i=0;i<m+1;i++) {DP[0][i]=1;}
for(int i=1;i<N+1;i++) {DP[i][0]=0;}
for(int i=1;i<(N+1);i++)
{
for(int j=1;j<m+1;j++)
{
if(i-S[j-1]>=0)
DP[i][j]=DP[i-S[j-1]][j]+DP[i][j-1];
else
DP[i][j]=DP[i][j-1];
}
}
return DP[N][m];
}
int main()
{
int N;
vector<int> S;
int m;
cout<<"Enter Total amount: ";
cin>>N;
cout<<"Enter number of dominations: ";
cin>>m;
int a;
cout<<"Enter dominations: ";
for(int i=0;i<m;i++)
{
cin>>a;
S.push_back(a);
}
cout<<"Number of ways is (Recur): " <<coin_change(N,S,m);
cout<<"\nNumber of ways is (DP): " <<coin_change_DP(N,S,m);
}