#322
Medium
Dynamic Programming

Coin Change

Find the minimum number of coins needed to make up an amount.

DP

Pattern fit

This is a great DP question because every amount depends on smaller amounts, and the recurrence forces you to consider all coin choices completely.

Key observation

For each amount a, the best answer is one more than the best answer of some reachable amount a - coin.

Target complexity

O(amount * coins) / O(amount)

How to break down the solution cleanly

1

This is a great DP question because every amount depends on smaller amounts, and the recurrence forces you to consider all coin choices completely.

2

For each amount a, the best answer is one more than the best answer of some reachable amount a - coin.

3

Name the state in plain language.

4

List the decisions that can transition into the state.

Reference implementation

Python
# Generic pattern template
# 1D DP
dp = [0] * (n + 1)
dp[0] = base
for i in range(1, n + 1):
    dp[i] = transition(dp, i)

# 2D DP
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    for j in range(1, m + 1):
        dp[i][j] = transition(dp, i, j)

Common pitfalls

warning

Using greedy coin picking on arbitrary denominations.

warning

Not initializing impossible states carefully.

Common follow-ups

How is this different from counting combinations?

What changes if each coin can be used only once?

Continue with related problems

Build repeatable depth inside the Dynamic Programming cluster before moving on.

view_weekBack to the pattern page
LeetCode 322. Coin Change Guide | Interview AiBox