-
Notifications
You must be signed in to change notification settings - Fork 0
/
322. Coin Change.py
35 lines (34 loc) · 976 Bytes
/
322. Coin Change.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
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
"""
minDict = {}
def dp(amount):
if amount == 0:
return 0
if amount < 0:
return -1
if amount in minDict:
if minDict[amount] != -1:
return minDict[amount]
else:
return -1
minSum = float("inf")
for coin in coins:
res = dp(amount - coin)
if res >= 0:
minSum = min(minSum, res)
if minSum != float("inf"):
minDict[amount] = minSum + 1
else:
minDict[amount] = -1
return minDict[amount]
return dp(amount)
s = Solution()
res = s.coinChange([2], 0)
print(res)