-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinsChange.cpp
92 lines (80 loc) · 2.34 KB
/
coinsChange.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
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
92
/*
Coin Change Problem
Send Feedback
For the given infinite supply of coins of each of denominations, D = {D0, D1, D2, D3, ...... Dn-1}. You need to figure out the total number of ways W, in which you can make the change for Value V using coins of denominations D.
Return 0 if the change isn't possible.
Input Format
The first line of the input contains an integer value N, which denotes the total number of denominations.
The second line of input contains N values, separated by a single space. These values denote the value of denomination.
The third line of the input contains an integer value, that denotes the value of V.
Output Format
Print the total total number of ways i.e. W.
Constraints :
1 <= n <= 10
1 <= V <= 1000
Time Limit: 1sec
Sample Input 1 :
3
1 2 3
4
Sample Output 1 :
4
Explanation to Sample Input 1 :
Number of ways are - 4 total i.e. (1,1,1,1), (1,1, 2), (1, 3) and (2, 2).
Sample Input 2 :
6
1 2 3 4 5 6
250
Sample Output 2 :
13868903
*/
#include <bits/stdc++.h>
using namespace std;
int countWaysToMakeChange(int* denominations, int numDenominations, int value, map<int, int> out){
//Write your code here
if(value == 0)
return 1;
if(value < 0)
return 0;
if(numDenominations == 0)
return 0;
int first, second;
if((out[value][numDenominations])>-1){
first = out[value][numDenominations];
}
else{
first = countWaysToMakeChange(denominations, numDenominations, value - denominations[0], out);
out[value][numDenominations] = first;
}
if(out[value][numDenominations]>-1){
second = out[value][numDenominations];
}
else{
second = countWaysToMakeChange(denominations+1, numDenominations-1, value, out);
out[value][numDenominations] = second;
}
return first + second;
}
int main()
{
int numDenominations;
cin >> numDenominations;
int *denominations = new int[numDenominations];
for (int i = 0; i < numDenominations; i++)
{
cin >> denominations[i];
}
int value;
cin >> value;
map <int, int> a;
for(int i=0;i<value;i++){
for(int j=0;j<numDenominations;j++)
a[i][j] = -1;
}
cout << countWaysToMakeChange(denominations, numDenominations, value, a);
// for(int i=0;i<value;i++){
// delete[] a[i];
// }
// delete[] a;
delete[] denominations;
}