forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinchange.cpp
More file actions
32 lines (31 loc) · 785 Bytes
/
coinchange.cpp
File metadata and controls
32 lines (31 loc) · 785 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int coinchange(vector<int>& a, int v, int n,
vector<vector<int> >& dp)
{
if (v == 0)
return dp[n][v] = 1;
if (n == 0)
return 0;
if (dp[n][v] != -1)
return dp[n][v];
if (a[n - 1] <= v) {
return dp[n][v] = coinchange(a, v - a[n - 1], n, dp)
+ coinchange(a, v, n - 1, dp);
}
else
return dp[n][v] = coinchange(a, v, n - 1, dp);
}
int32_t main()
{
int tc = 1;
while (tc--) {
int n, v;
n = 3, v = 4;
vector<int> a = { 1, 2, 3 };
vector<vector<int> > dp(n + 1,
vector<int>(v + 1, -1));
int res = coinchange(a, v, n, dp);
cout << res << endl;
}
}