TRT
Sign in
mediumAdvancedLeetCode#unbounded knapsack#minimum

Coin change

Find the fewest coins that make an amount, with unlimited copies of each coin, using an unbounded knapsack table.

The Runtime Theory Team1 min read
Taught inKnapsack and subsetsPro — members onlyChoosing a subset of items under a capacity is solved with a table over items and capacities. Whether each item can be used once or many times comes down to the direction of one loop.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

incoins = [1, 2, 5], amount = 11

out3

incoins = [2], amount = 3

out-1

incoins = [1], amount = 0

out0

Return the fewest coins needed to make the amount, or -1 if it can't be made. You have unlimited coins of each denomination.

Before you code

Greedy (largest coin first) fails for some coin sets. The table over amounts tries every coin as the last one.

Hints

  1. dp[0] = 0; all other amounts start at infinity.
  2. For each amount from 1 upward, dp[a] = min(dp[a - c] + 1) over coins c ≤ a.
  3. Return -1 if dp[amount] is still infinity.

Production & complexity notes

When solving Coin change on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Knapsack and subsets to reinforce the underlying pattern.

What it practices

The unbounded knapsack, from Knapsack and subsets.

Not started

Sign in to save your learning progress.

Sign in to save