-
Notifications
You must be signed in to change notification settings - Fork 3
/
money_change.cpp
49 lines (44 loc) · 1.3 KB
/
money_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
/**
* The goal in this problem is to find the minimum number of coins needed to
* change the input value (an integer) into coins with specified denominations.
*/
#include "money_change.hpp"
#include <limits>
#include <set>
#include <unordered_map>
using namespace std;
int money_change(int total, set<int> const& coins, unordered_map<int, int>& cache) {
if (cache.find(total) != cache.end()) {
return cache[total];
}
if (total == 0) {
return 0;
}
if (total < 0) {
throw invalid_argument("not defined for negative sum");
}
for (int coin : coins) {
if (total - coin < 0) {
continue;
}
if (total - coin == 0) {
cache[total] = 1;
break;
}
int coins_count = 1 + money_change(total - coin, coins, cache);
if (cache.find(total) != cache.end()) {
cache[total] = min(cache[total], coins_count);
} else {
cache[total] = coins_count;
}
}
if (cache.find(total) != cache.end()) {
return cache[total];
} else {
throw invalid_argument("no way to change " + to_string(total) + " coins");
}
}
int money_change(int total, set<int> const& coins) {
unordered_map<int, int> cache;
return money_change(total, coins, cache);
}