mediumDSA

How do you solve the coin change problem?

368 views
01

Understand the problem

Explain coin change DP.

dpcoin-change
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Min coins (bottom-up DP)
Run Playground
def coin_change(coins, amount):
    dp = [0] + [float('inf')] * amount
    for a in range(1, amount + 1):
        for c in coins:
            if c <= a:
                dp[a] = min(dp[a], dp[a - c] + 1)
    return dp[amount] if dp[amount] != float('inf') else -1


# --- demo ---
print(coin_change([1, 3, 4], 6))   # 2  (3+3, beats greedy's 4+1+1)
print(coin_change([2], 3))         # -1 (impossible)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.