-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
39 lines (37 loc) · 975 Bytes
/
program.cpp
File metadata and controls
39 lines (37 loc) · 975 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
33
34
35
36
37
38
39
#include "../include/pre.h"
#include <unordered_map>
class Solution {
private:
vector<int>* coins_handle;
std::unordered_map<int, int> mins;
int coinChange_helper(int amount)
{
if (amount < 0) return -1;
if (amount == 0) return 0;
if (mins.find(amount) != mins.end()) return mins[amount];
int32_t min = -2;
for (size_t i = 0; i != coins_handle->size(); i++)
{
uint32_t k = coinChange_helper(amount - coins_handle->at(i));
if (k == -1) continue;
min = std::min(static_cast<uint32_t>(min), k);
}
mins[amount] = min + 1;
return min + 1;
}
public:
int coinChange(vector<int>& coins, int amount)
{
coins_handle = &coins;
return coinChange_helper(amount);
}
};
int main()
{
TIC
auto s = Solution();
auto rst = s.coinChange(vector<int>{37,233,253,483}, 7163);
cout << rst << endl;
TOC
return 0;
}