forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0322.go
More file actions
32 lines (26 loc) · 631 Bytes
/
0322.go
File metadata and controls
32 lines (26 loc) · 631 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
26
27
28
29
30
31
32
var res int64
func coinChange(coins []int, amount int) int {
sort.Ints(coins)
res = int64(amount) + 1
dfs(coins, len(coins) - 1, int64(amount), 0)
if res > int64(amount) {
return -1
}
return int(res)
}
func dfs(coins []int, index int, cur, cnt int64) {
coins_i := int64(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 i := cur / coins_i; i >= 0; i-- {
dfs(coins, index - 1, cur - coins_i * i, cnt + i)
}
}