-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_change.cpp
62 lines (54 loc) · 1.51 KB
/
coin_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
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include<climits>
using namespace std;
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(amount <= 0)
return 0;
int max_size = amount;
int dp[max_size];
for(int i=0;i<max_size;i++)
dp[i]=-1;
for(int i = 0; i < coins.size();i++)
{
if(coins[i] > amount)
continue;
dp[coins[i] - 1]= 1;
}
for(int i=0;i<amount;i++)
{
if(dp[i]==1)
continue;
else
{
int min_count = INT_MAX;
for(int j=0;j<coins.size();j++)
{
int coin = coins[j];
if(coin > (i + 1))
continue;
int left = (i+1)-coin;
int coin_count = 0;
if(dp[left - 1] > 0)
coin_count = 1 + dp[left - 1];
if(coin_count < min_count && coin_count!=0)
min_count = coin_count;
}
if(min_count < INT_MAX)
dp[i]=min_count;
}
}
return dp[amount - 1];
}
};
int main()
{
Solution sol;
int vec[]={186,419,83,408};
vector<int> coins(vec,vec + sizeof(vec)/sizeof(vec[0]));
int cnt=sol.coinChange(coins,6249);
cout<<"coin count: "<<cnt<<"\n";
return 0;
}