forked from iamAnki/CPP-Programs-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoinChange.cpp
61 lines (48 loc) · 1.01 KB
/
CoinChange.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
//Coin Change
//DP
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//based completely on: count no. of subset with given sum + unbounded knapsack
long long count( int coin[], int m, int n )
{
long long dp[m+1][n+1];
for(int i=0; i<=m; i++)
dp[i][0] = 1;
for(int i=1; i<=n; i++)
dp[0][i] = 0;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
if(j >= coin[i-1])
{
dp[i][j] = dp[i-1][j] + dp[i][j-coin[i-1]] ;
}
else
{
dp[i][j] = dp[i-1][j];
}
}
}
return dp[m][n];
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int arr[m];
for(int i=0;i<m;i++)
cin>>arr[i];
Solution ob;
cout<<ob.count(arr,m,n)<<endl;
}
return 0;
}