Last active
June 5, 2021 15:31
-
-
Save derekli66/f4dc9022e2d8d82196ad4fc363b75448 to your computer and use it in GitHub Desktop.
Solution for 322 coin change problem.
This file contains hidden or 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
import typing | |
from typing import List | |
import sys | |
class Solution: | |
def __init__(self) -> None: | |
self.amount_memorization = {} | |
def coinChange(self, coins: List[int], amount: int) -> int: | |
miniCount = self.coinChange__(coins, amount) | |
if miniCount < sys.maxsize: | |
return miniCount | |
return -1 | |
def coinChange__(self, coins: List[int], amount: int) -> int: | |
if self.amount_memorization.get(amount) != None: | |
return self.amount_memorization[amount] | |
if amount == 0: | |
return 0 | |
miniStep = sys.maxsize | |
idx = 0 | |
while idx < len(coins): | |
if amount >= coins[idx]: | |
remainingAmount = amount - coins[idx] | |
# print(f"amount: {amount}. remainingAmount: {remainingAmount}") | |
currentStep = self.coinChange__(coins, remainingAmount) | |
miniStep = min(currentStep + 1, miniStep) | |
idx += 1 | |
if miniStep < sys.maxsize: | |
self.amount_memorization[amount] = miniStep | |
return miniStep | |
self.amount_memorization[amount] = sys.maxsize | |
return sys.maxsize | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment