forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0322.cpp
More file actions
24 lines (21 loc) · 731 Bytes
/
0322.cpp
File metadata and controls
24 lines (21 loc) · 731 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
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
sort(coins.begin(), coins.end(), greater<int>());
res = amount + 1;
dfs(coins, 0, amount, 0);
return res > amount ? -1 : res;
}
private:
long long res;
void dfs(vector<int>& coins, int index, long long target, long long cnt) {
if (cnt + (target + coins[index] - 1) / coins[index] >= res) return;
if (target % coins[index] == 0) {
res = cnt + target / coins[index];
return;
}
if (index == coins.size() - 1) return;
for (int i = target / coins[index]; i >= 0; i--)
dfs(coins, index + 1, target - coins[index] * i, cnt + i);
}
};