Coin Change

Context: FIT2004_MOC · the lecture’s first full DP, and the shape PT-02 questions wear as a costume — also where the decision array is introduced Parent Framework: Dynamic Programming

Quick Revision

  • 🎯 Objective: MinCoins[v] = {the fewest coins summing to exactly v} ➔ for each value, try every coin and take at its cheapest.
  • 📦 Core Components: subproblems ➔ coins tried at each ⟹ time, space (lecturer’s coins, target value).
  • ⚡ Key Constraint: greedy is wrong. On wanting , greedy takes coins; the optimum is . Coins are unlimited, so this is the unbounded shape ➔ Knapsack Problem.

📝 How It Works

1. Three Attempts, in the Lecture’s Order

  • Brute force ➔ try every combination, keep the smallest. “Will it work? Of course!” — and it is exponential, which is the motivation for everything after it.
  • Greedy ➔ take the biggest coin that fits, then fill the balance. Does not always work: currency , target ⟹ greedy (4 coins) vs optimal (2 coins).
  • Why greedy dies here ➔ committing to the leaves a remainder that only s can pay; the greedy-choice property fails, but optimal substructure survives ⟹ DP ➔ Greedy Algorithm §2.
  • Dynamic programming ➔ solve every value from up to the target, reusing the smaller answers. The lecture shrinks the demo from / to / “easier for me to visualise” — do the same in an exam: a small instance you can finish beats a large one you cannot.

2. The Subproblem and the Recurrence

  • MEMO sentence ➔ MinCoins[v] = {the minimum number of coins needed to make the value v}, for .
  • The value is the subproblem; the coins are the choices ➔ cells (the is the base case), and the coins are the work inside each cell. Counting the coins as a dimension is the standard complexity error ➔ Dynamic Programming §5.
  • Base ➔ costs coins, unambiguously.
  • Edge ➔ no coin fits ⟹ , meaning unreachable. is a legitimate answer, not a placeholder — which is exactly why it cannot double as the “not yet computed” marker (§4).
  • General ➔ “which coin did I add last?” Every choice of last coin reduces the problem to a value already solved.
  • Answer ➔ , a fixed cell.

3. Filling the Table by Hand

  • Sweep values upward, and at each value loop over every coin ➔ “we will loop through this over and over considering the coins.”
  • Every write is a comparison against what is already there ➔ at the table holds (five s) and the coin offers ; the takes .
  • Early termination on a sorted coin list ➔ once the rest of a sorted list cannot fit either, so break. It does not change but it is the lecturer’s stated optimisation.
  • Cost ➔ values coins time, auxiliary — “still much faster than brute force.”

4. Top-Down vs Bottom-Up — and the Sentinel Trap

  • The lecture gives the top-down form ➔ recursive, memoised, initialised to for “not computed yet”.
  • , never ➔ is a real answer for an unreachable value, so using it as the sentinel makes an unreachable subproblem look uncomputed and it is recomputed forever. Pick a sentinel outside the answer’s range.
  • The bottom-up body is the same expression ➔ ; only the control flow changes.
  • The trade-off as stated ➔ top-down might save some computations (it only visits reachable subproblems) · bottom-up might save space, especially since there is no recursion (no call stack).
  • The lecturer’s own habit ➔ “I only use bottom-up”, while noting some problems are more intuitive top-down and the two are technically interchangeable.

5. Reconstruction — the Decision Array (the lecture’s vehicle for it)

  • Storing the whole combination per cell is ➔ writing , , … at every value duplicates the prefix over and over.
  • Store only the LAST coin added ➔ one value per cell ⟹ , and the full combination is recovered by walking backwards.
  • The walk ➔ from , emit , jump to , repeat until .
  • Worked ➔ ; . Coins .
  • Decision array vs backtracking ➔ the array is faster (the decision is read, not re-derived); backtracking uses less auxiliary space at the same space complexity. Both still pay to emit the solution ➔ Dynamic Programming §8.

⚙️ Core Implementation

🔹 Top-down, memoised (the lecture’s algorithm)

🔹 Bottom-up, with the decision array

⚖️ Complexity

coin denominations, target value .

ApproachTimeAuxiliary spaceNote
Brute forceexponential stackevery combination
Greedyincorrect on , target
DP, bottom-upbest average worst
DP, top-down stackmay skip unreachable subproblems
decision array extrareconstruction in
full combination per celldo not — the lecture’s rejected version
  • Both parameters must be named ➔ is meaningless without ” denominations, target value”; is a numeric value, so its bit-length matters if the input is a number ➔ Algorithmic Complexity §Input size.

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace

Currency , target .

Value
Number of coins
Last coin added—

Final Extracted Output: ; backtracking the decision array gives coins .

  • Read the overwrite ➔ is first written as (five s) and then beaten by the coin at ; likewise falls from (5+1) to (the coin 6). Every cell is a running minimum, not a single write.
  • Read the non-monotonicity ➔ while . A bigger target can be cheaper; never assume the row rises.
  • Read the greedy failure right off the table ➔ greedy would go (then ), landing on for a total of . The DP finds .

Applied Exercise — the PT-02 costume test

Problem: microwave keys , minimum presses to enter time . Show it is coin change.

Final Extracted Output: if ; if ; else — sanity-checked at (two s, five s). The reusable move (LO1): name the classical problem before writing anything; every PT-02 question is one of the pass-level six wearing different nouns ➔ Dynamic Programming §4.

⚠️ Common Mistakes

  • 💡 Assuming greedy works because it works on real currency ➔ happens to be canonical; the exam picks precisely because it is not. Never claim greedy without a proof ➔ Greedy Algorithm §5.
  • 💡 as the memo sentinel ➔ collides with the legitimate unreachable answer; use .
  • 💡 Quoting time ➔ that is the table size; each cell tries coins, so time is and space is .
  • 💡 Reporting only the count when asked for the coins ➔ keep the decision array (or backtrack); “what are the coins?” is a different question ➔ §5.
  • 💡 Storing the full combination at every value ➔ space for information the last-coin array already determines.

🧠 Active Recall