Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 19, 2017 18:45
Show Gist options
  • Save cixuuz/e383d1c66294ddc044a77e1a8e3ca529 to your computer and use it in GitHub Desktop.
Save cixuuz/e383d1c66294ddc044a77e1a8e3ca529 to your computer and use it in GitHub Desktop.
[322. Coin Change] #leetcode
class Solution(object):
# O(n) O(n)
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""
# corner case
if not coins:
return -1
if not amount:
return 0
# dp placeholder
dp = [0] * (amount+1)
for x in range(1, amount+1):
# f(x) = min(f(x- ci)) + 1
temp = [dp[x-c] for c in coins if x-c >= 0 and dp[x-c] != -1]
dp[x] = min(temp) + 1 if temp else -1
return dp[amount]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment