forked from ProAlgos/ProAlgos-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin_change.hpp
70 lines (54 loc) · 2.12 KB
/
coin_change.hpp
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
/*
Coin Change Algorithm
---------------------
Given an unlimited amount of coins with different values, find the number of ways of making change for an given
value using the coins.
Time complexity
---------------
O(M*N), where M is the number of coins with different values and N is the amount that we desired to change
Space complexity
----------------
O(M*N), where M is the number of coins with different values and N is the amount that we desired to change
*/
#ifndef COIN_CHANGE_HPP
#define COIN_CHANGE_HPP
#include <vector>
using std::vector;
int coin_change(const vector<int>& coin, int number_of_coins, int amount)
{
if (number_of_coins == 0 && amount == 0) {
return 1;
} else if (number_of_coins == 0) {
return 0;
}
int i, j;
// Make a matrix of size (amount+1)*number_of_coins to tabulate the computed values
// table[i][j] represents no.of ways in which an 'i' can be made with just j type of coins available
vector<vector<int>> table (amount + 1, vector<int>(number_of_coins, 0));
int current_in; // Stores the number of ways in which the amount can be changed including the current coin value
int current_out; // Stores the number of ways in which the amount can be changed excluding the current coin value
for (j = 0; j < number_of_coins; j++) {
table[0][j] = 1;
}
// i represents the amount whose change is required
// j represents the coin values available
for (i = 1; i <= amount; i++) {
for (j = 0; j < number_of_coins; j++) {
// If the the value of current coin is less than or equal to total amount whose change is required
// include this coin
if (i >= coin[j]) {
current_in = table[i - coin[j]][j];
} else {
current_in = 0;
}
if (j >= 1) {
current_out = table[i][j - 1];
} else {
current_out = 0;
}
table[i][j] = current_in + current_out;
}
}
return table[amount][number_of_coins - 1];
}
#endif // COIN_CHANGE_HPP