Skip to content

Commit

Permalink
2020-03-07
Browse files Browse the repository at this point in the history
  • Loading branch information
JiayangWu committed Mar 8, 2020
1 parent 4d71f92 commit 51055d5
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions 0322.零钱兑换/0322-零钱兑换.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ def coinChange(self, coins, amount):
:type amount: int
:rtype: int
"""
if amount == 0:
return 0
dp = list()
max_int = 2 << 31
from collections import deque

for i in range(amount + 1):
if i not in coins:
dp.append(max_int)
else:
dp.append(1)

for i in range(amount + 1):
if i not in coins:
for j in coins:
if i - j > 0:
dp[i] = min(dp[i - j] + 1, dp[i])
queue = deque([(0, 0)])
visited = set([0])
while queue:
cur, step = queue.popleft()
if cur == amount:
return step
if cur > amount:
continue

for coin in coins:
value = cur + coin
if value not in visited:
visited.add((value))
queue.append((value, step + 1))

return dp[amount] if dp[amount] != max_int else -1

return -1

0 comments on commit 51055d5

Please sign in to comment.