This problem follows the Unbounded Knapsack pattern, commonly found in the 1-D Dynamic Programming category. Recognizing this pattern is key to solving it efficiently in an interview setting.
dp[amount] = min(dp[amount], dp[amount - coin] + 1) for each coin.
This is unbounded knapsack — each coin can be used unlimited times. The order of the loops (amount outer, coins inner) handles this correctly.
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
for coin in coins:
if coin <= i:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1Practice Coin Change and similar 1-D Dynamic Programming problems with flashcards. Build pattern recognition through active recall.
Practice this problem