forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin-Change-Problem-DP.cpp
More file actions
91 lines (76 loc) · 1.45 KB
/
Coin-Change-Problem-DP.cpp
File metadata and controls
91 lines (76 loc) · 1.45 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+2;
// int dp[N][N];
// int CoinChange(vector<int> &a,int n,int x)
// {
// if(x==0)
// {
// return 1;
// }
// if(x<0)
// {
// return 0;
// }
// if(n<=0)
// {
// return 0;
// }
// if(dp[n][x]!=-1)
// {
// return dp[n][x];
// }
// dp[n][x] = CoinChange(a,n,x-a[n-1]) + CoinChange(a,n-1,x);
// return dp[n][x];
// }
// int main()
// {
// int n;
// cin>>n;
// for(int i=0;i<N;i++)
// {
// for(int j=0;j<N;j++)
// {
// dp[i][j]=-1;
// }
// }
// vector<int>a(n);
// for(int i=0;i<n;i++)
// {
// cin>>a[i];
// }
// int x;
// cin>>x;
// cout<<CoinChange(a,n,x)<<endl;
// return 0;
// }
int main()
{
int n;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int x;
cin>>x;
// dp table (v*x)
vector<vector<int>> dp(n+1,vector<int>(x+1,0));
dp[0][0]=1;
for(int i=1;i<n+1;i++)
{
for(int j=0;j<x+1;j++)
{
// when taken
if(j-a[i-1]>=0)
{
dp[i][j] += dp[i][j-a[i-1]];
}
//when not taken
dp[i][j] += dp[i-1][j];
}
}
cout<<dp[n][x];
return 0;
}