---
title: "LeetCode #518: Coin Change II"
url: https://perrotta.dev/2025/12/leetcode-%23518-coin-change-ii/
last_updated: 2026-01-03
---


[LeetCode #518: Coin Change II](https://leetcode.com/problems/coin-change-ii):

```python
class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        from functools import cache

        @cache
        def solve(amount, i):
            if amount < 0:
                return 0

            if amount == 0:
                return 1

            if i < 0:
                return 0

            return sum((
                solve(amount - coins[i], i),
                solve(amount, i - 1),
            ))

        return solve(amount, len(coins) - 1)
```

