-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
31 lines (21 loc) · 868 Bytes
/
12.py
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
import math
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
def recur(coins, amount, index, dp):
if amount == 0:
return 0
if index >= len(coins):
return math.inf
if dp[index][amount] != -1:
return dp[index][amount]
count1 = math.inf
if coins[index] <= amount:
count1 = recur(coins, amount - coins[index], index, dp)
if count1 != math.inf:
count1 += 1
count2 = recur(coins, amount, index + 1, dp)
dp[index][amount] = min(count1, count2)
return dp[index][amount]
dp = [[-1 for _ in range(amount + 1)] for _ in range(len(coins))]
res = recur(coins, amount, 0, dp)
return -1 if res == math.inf else res