-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path322_Coin_Change.cpp
73 lines (73 loc) · 1.68 KB
/
322_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
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
if(coins.size()==0)
return -1;
vector<int> nums(coins.size(),0);
sort(coins.begin(),coins.end());
int remain=amount,curCoin=coins.size()-1,sum=0,min=-1;
bool needBack=false;
//cout<<remain<<endl;
while(true)
{
nums.at(curCoin)=remain/coins.at(curCoin);
sum+=remain/coins.at(curCoin);
//cout<<"curCoin: "<<curCoin<<" nums.at(curCoin): "<<nums.at(curCoin)<<endl;
remain=remain%coins.at(curCoin);
if(remain==0)
{
if(min==-1)
min=sum;
else
min=min>sum?sum:min;
//cout<<"min= "<<min<<endl;
needBack=true;
}
else if((curCoin==0&&remain!=0)||(min>=0&&sum>=min))
{
//cout<<"remain= "<<remain<<endl;
needBack=true;
}
else
needBack=false;
if(needBack)
{
if(curCoin==0||(min>=0&&sum>=min))
{
remain+=nums.at(curCoin)*coins.at(curCoin);
sum-=nums.at(curCoin);
nums.at(curCoin)=0;
curCoin++;
}
//cout<<"what tf\n"<<"sum = "<<sum<<" remain= "<<remain<<" curCoin= "<<curCoin<<endl;
//for(int i=0;i<nums.size();i++)
// cout<<nums.at(i)<<" ";
//cout<<endl;
while(curCoin<nums.size()&&nums.at(curCoin)==0)curCoin++;
//cout<<curCoin<<" "<<nums.at(curCoin)<<" "<<remain<<endl;
if(curCoin>=nums.size())
return min;
nums.at(curCoin)--;
sum--;
remain+=coins.at(curCoin);
}
curCoin--;
}
return min;
}
};
int main()
{
Solution s;
vector<int> coins;
coins.push_back(186);
coins.push_back(419);
coins.push_back(83);
coins.push_back(408);
cout<<s.coinChange(coins,6249)<<endl;
return 0;
}