Guest User

Untitled

a guest
Oct 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class Solution(object):
  2. # O(n) O(n)
  3. def coinChange(self, coins, amount):
  4. """
  5. :type coins: List[int]
  6. :type amount: int
  7. :rtype: int
  8. """
  9. # corner case
  10. if not coins:
  11. return -1
  12. if not amount:
  13. return 0
  14.  
  15. # dp placeholder
  16. dp = [0] * (amount+1)
  17.  
  18. for x in range(1, amount+1):
  19. # f(x) = min(f(x- ci)) + 1
  20. temp = [dp[x-c] for c in coins if x-c >= 0 and dp[x-c] != -1]
  21. dp[x] = min(temp) + 1 if temp else -1
  22.  
  23. return dp[amount]
Add Comment
Please, Sign In to add comment