Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Fewest Coins

Learn
Play
Prove
Problem context and objectives
Mission briefing

Energy-route planner · A runner crosses a line of power stations

Build each decision from smaller outcomes already solved: The Fewest Coins.

Re-solving the same future branches drains exponential energy.

How you win

  1. 1Recognize when Minimum coin change matches the clues
  2. 2Keep this true after every move: dp[amount] is the fewest coins known to form that exact amount
  3. 3Reach the result within O(amount times coin count) time and O(amount) space

Rules and pressure

  • Target cost: O(amount times coin count) time and O(amount) space
  • State rule: dp[amount] is the fewest coins known to form that exact amount
Lesson 1 of 3

Live algorithm trace

Minimum coin change

Complete execution
1 of 6

Seed amount zero and mark positive amounts unreachable.

1dp[0] = 0; others infinity
2for amount from 1 through target
3for each coin not exceeding amount
4candidate = dp[amount - coin] + 1
5keep the minimum candidate
6return -1 if target remains infinity
target = 11dp0 = 0
Truth to preserve / Cost target
Truth to preserve

dp[amount] is the fewest coins known to form that exact amount

Cost target

O(amount times coin count) time and O(amount) space

For every amount, try making one final coin choice after an already solved smaller amount. Unlimited reuse is natural because that smaller state may contain the same coin.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead