Coin Change

interview_workbook/leetcode/dp_1d /app/src/interview_workbook/leetcode/dp_1d/coin_change.py
View Source

Algorithm Notes

Summary: Coin Change — notes not yet curated.
Time: Estimate via loops/recurrences; common classes: O(1), O(log n), O(n), O(n log n), O(n^2)
Space: Count auxiliary structures and recursion depth.
Tip: See the Big-O Guide for how to derive bounds and compare trade-offs.

Big-O Guide

Source

"""
Coin Change

TODO: Add problem description
"""

from src.interview_workbook.leetcode._registry import register_problem
from src.interview_workbook.leetcode._types import Category, Difficulty


class Solution:
    def solve(self, *args) -> int:
        """Return the fewest coins needed to make up the amount or -1 if not possible."""
        if len(args) != 2:
            return -1
        coins, amount = args
        dp = [amount + 1] * (amount + 1)
        dp[0] = 0
        for a in range(1, amount + 1):
            for c in coins:
                if a - c >= 0:
                    dp[a] = min(dp[a], dp[a - c] + 1)
        return dp[amount] if dp[amount] != amount + 1 else -1


def demo():
    """Run a demo for the Coin Change problem."""
    solver = Solution()
    coins = [1, 2, 5]
    amount = 11
    result = solver.solve(coins, amount)
    return str(result)


register_problem(
    id=322,
    slug="coin_change",
    title="Coin Change",
    category=Category.DP_1D,
    difficulty=Difficulty.MEDIUM,
    tags=["array", "dynamic_programming", "bfs"],
    url="https://leetcode.com/problems/coin-change/",
    notes="",
)