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