Created
October 19, 2017 18:45
-
-
Save cixuuz/e383d1c66294ddc044a77e1a8e3ca529 to your computer and use it in GitHub Desktop.
[322. Coin Change] #leetcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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