thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

LeetCode #518: Coin Change II

β€’ 58 words β€’ 1 min β€’ updated

LeetCode #518: 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)