<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:
        dp = [[float("inf")] * (amount + 1) for _ in range(len(coins) + 1)]

        for i in range(len(coins)+1):
            dp[i][0] = 0

        for i in range(1, len(coins) + 1):
            for j in range(1, amount + 1):
                if coins[i-1] <= j:
                    dp[i][j] = min(dp[i-1][j], 1 + dp[i][j - coins[i-1]])
                else:
                    dp[i][j] = dp[i-1][j]
        
        return dp[len(coins)][amount] if dp[len(coins)][amount] != float("inf") else -1
    
    def coinChange(self, coins: List[int], amount: int) -> int:

        dp = [[-1] * (amount + 1) for _ in range(len(coins) + 1)]
        
        def dfs(idx: int, remaining_amount: int) -> int:
            # Base case: if the amount is zero, we found a valid solution (0 coins more needed)
            if remaining_amount == 0:
                return 0
                
            if idx == len(coins):
                return float("inf")

            if dp[idx][remaining_amount] != -1:
                return dp[idx][remaining_amount]

            include = float("inf")
            if coins[idx] <= remaining_amount:
                include = 1 + dfs(idx, remaining_amount - coins[idx])
            exclude = dfs(idx + 1, remaining_amount)
            dp[idx][remaining_amount] = min(exclude, include)

            return dp[idx][remaining_amount]
            
        result = dfs(0, amount)
        return result if result != float("inf") else -1

    def _coinChange(self, coins: List[int], amount: int) -> int:
        # Initialize dp table for memoization with -1 (indicating uncomputed states)
        dp = [[-1] * (amount + 1) for _ in range(len(coins) + 1)]
        
        def dfs(i: int, amount: int) -> int:
            # Base case: If the amount is 0, no coins are needed
            if amount == 0:
                return 0
            # If no coins left but amount > 0, it's not possible to make the amount
            if i == 0:
                return float('inf')
            
            # Check memoized result
            if dp[i][amount] != -1:
                return dp[i][amount]
            
            # Recursive case: two options, either take the current coin or skip it
            if coins[i - 1] <= amount:
                # Option 1: Take the coin and reduce the amount, stay on the same coin (i)
                take = 1 + dfs(i, amount - coins[i - 1])
                # Option 2: Don't take the coin, move to the previous coin (i-1)
                skip = dfs(i - 1, amount)
                dp[i][amount] = min(take, skip)  # Choose the minimum
            else:
                # Skip the current coin since it's greater than the amount
                dp[i][amount] = dfs(i - 1, amount)
            
            return dp[i][amount]
        
        # Call the recursive function starting from the last coin
        result = dfs(len(coins), amount)
        
        # If the result is infinity, it means it's not possible to make the amount
        return result if result != float('inf') else -1
    
    def _coinChange(self, coins: List[int], amount: int) -> int:
        dp = [-1] * (amount + 1)

        def dfs(amount):
            if amount == 0:
                return 0
            
            if amount < 0:
                return float("inf")
            if dp[amount] != -1:
                return dp[amount]

            low = float("inf")
            for coin in coins:
                res = dfs(amount - coin)
                if res != float("inf"):
                    low = min(low, 1 + res) 
            dp[amount] = low
            return low
        
        res = dfs(amount)
        return res if res != float("inf") else -1


        
        
45683