-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoin-change.cpp
57 lines (45 loc) · 925 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
/**
* @file coin-change.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-29
*
* @copyright Copyright (c) 2021
*
*/
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <vector>
using namespace std;
int naive(vector<int> denom, int n, int m) {
if (n == 0) {
return 1;
}
if (n < 0) {
return 0;
}
if (m <= 0 && n >= 1) {
return 0;
}
return naive(denom, m - 1, n) + naive(denom, m, n - denom[m - 1]);
}
int number_of_ways(vector<int> denom, int k) {
int n = denom.size();
int dp[k + 1];
memset(dp, 0, sizeof(dp));
dp[0] = 1;
for (int j = 0; j < n; j++) {
for (int i = denom[j]; i <= k; i++) {
dp[i] += dp[i - denom[j]];
}
}
return dp[k];
}
int main(int argc, const char **argv) {
vector<int> denominations = {1, 6, 10};
int k = 20;
cout << number_of_ways(denominations, k);
return 0;
}