-
Notifications
You must be signed in to change notification settings - Fork 7
/
coin-change.cpp
64 lines (61 loc) · 1010 Bytes
/
coin-change.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
63
64
//https://www.hackerrank.com/challenges/coin-change
//mandeep singh @msdeep14
#include<iostream>
#include<string.h>
using namespace std;
/*
int count(int s[],int m,int n)
{
if(n==0)
return 1;
if(n<0)
return 0;
if(m<=0 && n>=1)
return 0;
return count(s,m-1,n)+count(s,m,n-s[m-1]);
}
*/
/*
int count(int s[],int m,int n)
{
int table[n+1];
memset(table,0,sizeof(table));
table[0]=1;
for(int i=0;i<m;i++)
{
for(int j=s[i];j<=n;j++)
{
table[j]+=table[j-s[i]];
}
}
return table[n];
}
*/
long long int count(long long int s[],long long int m,long long int n)
{
long long int table[n+1][m];
long long int x,y;
for(int i=0;i<m;i++)
table[0][i]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
{
x=(i-s[j]>=0)?table[i-s[j]][j]: 0;
y=(j>=1)?table[i][j-1]: 0;
table[i][j]=x+y;
}
}
return table[n][m-1];
}
int main()
{
long long int m,n;
long long int arr[10000];
cin>>n>>m;
for(int i=0;i<m;i++)
cin>>arr[i];
long long int x=count(arr,m,n);
cout<<x;
return 0;
}