forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0322.py
More file actions
22 lines (17 loc) · 660 Bytes
/
0322.py
File metadata and controls
22 lines (17 loc) · 660 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
def coinChange(self, coins, amount):
coins.sort(reverse=True)
n, res = len(coins), amount + 1
def dfs(index, target, cnt):
nonlocal res
if cnt + (target + coins[index] - 1) // coins[index] >= res:
return
if target % coins[index] == 0:
res = cnt + target // coins[index]
return
if index == n - 1:
return
for i in range(target // coins[index], -1, -1):
dfs(index + 1, target - coins[index] * i, cnt + i)
dfs(0, amount, 0)
return -1 if res > amount else res