-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCoinChange.cpp
53 lines (45 loc) · 1.26 KB
/
CoinChange.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
// Minimum number of coins need to make up that amount
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/111/dynamic-programming/809/
#include <vector>
#include <iostream>
#include <algorithm>
#include <climits>
void print(const std::vector<int>& coins)
{
for (auto coin : coins) {
std::cout << coin << " ";
}
}
int minCoins(std::vector<int>& coins, const int amount)
{
std::vector<int> nums(amount + 1, INT_MAX);
nums[0] = 0;
std::sort(coins.begin(), coins.end());
for (int i = 1; i <= amount; ++i) {
for (auto c : coins) {
if (i - c < 0) {
break;
}
if (nums[i - c] != INT_MAX) {
nums[i] = std::min(nums[i], nums[i - c] + 1);
}
}
}
return ((nums[amount] == INT_MAX) ? -1 : nums[amount]);
}
void test(std::vector<int>& coins, const int amount)
{
std::cout << "Coins : ";
print(coins);
std::cout << "\nMin num of coins to make " << amount << " : ";
std::cout << minCoins(coins, amount);
std::cout << "\n===================================\n";
}
int main()
{
std::vector<int> coins1 {1,2,5};
test(coins1, 11);
std::vector<int> coins2 {6};
test(coins2, 7);
return 0;
}