-
Notifications
You must be signed in to change notification settings - Fork 0
/
322.CoinChange.py
36 lines (35 loc) · 969 Bytes
/
322.CoinChange.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
32
33
34
35
36
class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
coins = [1,2,5], amount = 11
5 + 5 + 1
"""
if amount == 0:
return 0
from collections import defaultdict
mymap = defaultdict()
for i in coins:
mymap[i] = 1
minval = min(coins)
def helper(total):
if total in mymap:
return mymap[total]
if total < minval:
return -1
res = float("inf")
for i in coins:
tmp = helper(total - i)
if tmp == -1:
continue
res = min(res, tmp + 1)
mymap[total] = res
if res == float("inf"):
return -1
return res
return helper(amount)
sol = Solution()
res = sol.coinChange([2], 3)
print(res)