> ## 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 II Python Solution with Tests

> Tested Python solution for LeetCode 518 with 14 pytest cases. Generate a practice environment with lcpy.

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

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

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 518   # by problem number
lcpy gen -s coin_change_ii   # 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 number of combinations that make up that amount*. If that amount of money cannot be made up by any combination of the coins, return `0`.

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

The final answer is **guaranteed** to fit into a signed 32-bit integer.

### Examples

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

**Explanation:** there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1

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

**Explanation:** the amount of 3 cannot be made up just with coins of 2.

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

### Constraints

* 1 \<= coins.length \<= 300
* 1 \<= coins\[i] \<= 5000
* All the values of `coins` are **unique**.
* 0 \<= amount \<= 5000

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * amount)
    # Space: O(amount)
    def change(self, amount: int, coins: list[int]) -> int:
        # dp[a] = number of combinations summing to a
        dp = [0] * (amount + 1)
        dp[0] = 1
        for coin in coins:
            for a in range(coin, amount + 1):
                dp[a] += dp[a - coin]
        return dp[amount]
```

## Complexity

| Time           | Space     |
| -------------- | --------- |
| O(n \* amount) | O(amount) |

## Tags

[NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
