<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>

<p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p>

<p>You may assume that you have an infinite number of each kind of coin.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> coins = [1,2,5], amount = 11
<strong>Output:</strong> 3
<strong>Explanation:</strong> 11 = 5 + 5 + 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> coins = [2], amount = 3
<strong>Output:</strong> -1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> coins = [1], amount = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= coins.length &lt;= 12</code></li>
	<li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li>
</ul>

from functools import lru_cache

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        memo = {}
        def recurse(amount):
            output = float('inf')
            if amount in memo:
                return memo[amount]
            if amount == 0:
                # print('true')
                output = 0
            if amount < 0:
                output = None 
            else:
                for coin in coins:
                    if recurse(amount-coin) is not None:
                        # print(amount-coin)
                        output = min(output, recurse(amount-coin) +1)
                        # print(output)
            memo[amount] = output
            return output
        temp = recurse(amount)
        # print(memo)
        return temp if temp != float('inf') else -1














        
        # dp = {}
        # # debug this thing!!!!!!
        # def backtrack(amount, count):
        #     mincount = float('inf')
        #     if amount in dp:
        #         return dp[amount]
        #     if amount == 0:        
        #         mincount = min(mincount, count)
        #         print('mincount is ' +str(mincount))
        #     if amount < 0:
        #         return
        #     for coin in coins:
        #         print(coin)
        #         backtrack(amount - coin, count +1)
        #     dp[amount] = mincount
        #     return dp[amount]
        # backtrack(amount, 0)
        # # print(dp)
        # return dp[amount] if dp[amount] != float('inf') else -1











        # # if amount == 0:
        # #     return 0
        # # # bottom up dp 
        # # m = len(coins)
        # # n = amount
        # # dp = [[float('inf')] * (n+1)] * m
        # # for i in range(m):
        # #         dp[i][0] = 0
        # # for j in range(n+1):
        # #     for i in range(m):
        # #         if j - coins[i] >= 0 and dp[i][j-coins[i]] != float('inf'):
        # #             print(dp[i][j-coins[i]])
        # #             dp[i][j] = min(dp[i][j], dp[i][j-coins[i]]+1)
        # #         # dp[i][j] = min(dp[i][j], dp[i-1][j]) # is this necessary??
        # # # print(dp)
        # # return dp[-1][-1] if dp[-1][-1] != float('inf') else -1 











        # # # # but first, recursion:
        # # # @lru_cache(None)
        # # # # coins = sorted(coins, reverse=True)

        # # # def recurse(amount):
        # # #     mincount = float('inf')
        # # #     if amount < 0:
        # # #         return None
        # # #     if amount == 0:
        # # #         return 0
        # # #     for coin in coins:
        # # #         temp = recurse(amount-coin)
        # # #         print(temp)
        # # #         if temp: # note this is the mistake, the second last line would return -1
        # # #             mincount = min(mincount, temp+1)
        # # #     return mincount if mincount != float('inf') else -1

        # # # return recurse(amount)




        # # # recursion 
        # # # if amount == 0:
        # # #     print(amount)
        # # #     return 0
        # # # def recurse(coins, amount, count):
        # # #     mincount = float('inf')
        # # #     if amount <0:
        # # #         return None
        # # #     if amount == 0:
        # # #         return count
        # # #     else:
        # # #         for i in coins:
        # # #             temp =recurse(coins, amount - i, count+1)
        # # #             if temp:
        # # #                 mincount= min(mincount, temp)
        # # #         return mincount
        # # # temp = recurse(coins, amount, 0) 
        # # # return temp if temp != float('inf') else -1













        # # # dp = [float('inf')] * (amount +1)
        # # # dp[0] = 0
        # # # for i in range(amount+1):
        # # #     for j in coins:
        # # #         if j <= i:
        # # #             dp[i] = min(dp[i], dp[i-j]+1)
        # # # return -1 if dp[-1] == float('inf') else dp[-1]

47696