<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>

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        self.early_returns = {}
        self.currentAnswer = amount + 1
        if amount %2:
            foundOdd = False
            for coin in coins:
                if coin %2:
                    foundOdd = True
                    break
            if not foundOdd:
                return -1
        coins.sort()
        result, answer = self.coinChangeSorted(coins, amount, 0)
        return answer if result else -1

    def coinChangeSorted(self, coins: List[int], amount: int, coinsUsed: int) -> int:
        if not coins:
            return False, -1
        if coinsUsed >= self.currentAnswer:
            return False, -1
        l = len(coins)
        if l in self.early_returns:
            if amount in self.early_returns[l]:
                return self.early_returns[l][amount]
        maxCoinsForLast = amount // coins[-1]
        maxCoinsForLast = min(maxCoinsForLast, self.currentAnswer - coinsUsed)
        # if amount % coins[-1] == 0:
        if maxCoinsForLast * coins[-1] == amount:
            if l not in self.early_returns: 
                self.early_returns[l] = {}
            self.early_returns[l][amount] = [True, maxCoinsForLast]
            self.currentAnswer = min(self.currentAnswer, coinsUsed + maxCoinsForLast)
            return True, maxCoinsForLast
        foundAnswer = False
        answer = amount
        for coinsForLast in range(maxCoinsForLast, -1, -1):
            val, numCoins = self.coinChangeSorted(coins[:-1], amount - coins[-1] * coinsForLast, coinsUsed + coinsForLast)
            if val:
                foundAnswer = True
                answer = min(answer, numCoins + coinsForLast)
        if foundAnswer:
            if l not in self.early_returns: 
                self.early_returns[l] = {}
            self.early_returns[l][amount] = [foundAnswer, answer]
            self.currentAnswer = min(self.currentAnswer, coinsUsed + answer)
            return foundAnswer, answer
        if l not in self.early_returns: 
            self.early_returns[l] = {}
        self.early_returns[l][amount] = [False, -1]
        return False, -1

25558