> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Coin Change Python Solution with Tests

> Tested Python solution for LeetCode 322 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 322, Medium. Topics: Array, Dynamic Programming, Breadth-First Search. [View on LeetCode](https://leetcode.com/problems/coin-change/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 322   # by problem number
lcpy gen -s coin_change   # by problem name
```

## Problem

You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return `-1`.

You may assume that you have an infinite number of each kind of coin.

### Examples

```
Input: coins = [1,2,5], amount = 11
Output: 3
```

**Explanation:** 11 = 5 + 5 + 1

```
Input: coins = [2], amount = 3
Output: -1
```

```
Input: coins = [1], amount = 0
Output: 0
```

### Constraints

* `1 <= coins.length <= 12`
* `1 <= coins[i] <= 2^31 - 1`
* `0 <= amount <= 10^4`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/coin_change/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/coin_change/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(amount * len(coins))
    # Space: O(amount)
    def coin_change(self, coins: list[int], amount: int) -> int:
        if amount == 0:
            return 0

        # Initialize dp array with amount + 1 (impossible value)
        # Since max coins needed is amount (using all 1-cent coins)
        # amount + 1 serves as "infinity" to indicate impossible cases
        dp = [amount + 1] * (amount + 1)

        dp[0] = 0

        for i in range(1, amount + 1):
            for coin in coins:
                if coin <= i:
                    dp[i] = min(dp[i], dp[i - coin] + 1)

        # Return result: -1 if impossible, otherwise minimum coins needed
        return dp[amount] if dp[amount] <= amount else -1
```

## Complexity

| Time                    | Space     |
| ----------------------- | --------- |
| O(amount \* len(coins)) | O(amount) |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
