VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 12: Dynamic programming

Lesson 12.4 of 16 in this module Unrolled Pro

Coin Change

The fewest coins that add up to an amount, with unlimited coins of each denomination. From trying every combination recursively (exponential), past the greedy 'largest coin first' that fails on some coin systems, to a DP over amounts where best[a] = 1 + min best[a − c] (O(amount · coins)), and BFS over amounts as an equivalent view. Then a limited number of each coin, recovering which coins were used, counting the ways instead, and packing a batch from the micro-batch sizes a kernel supports.

about 10 min 3 Python examples 2 quick checks

Problem. Given coin denominations coins and an amount, return the fewest coins that add up to the amount, using any number of each coin, or −1 if it can't be made.

text
coins = [1, 2, 5],  amount = 11   →   3     (5 + 5 + 1)
coins = [2],        amount = 3    →   −1
coins = [1, 3, 4],  amount = 6    →   2     (3 + 3; greedy takes 4 + 1 + 1)

The state is the amount still to make. Whatever the last coin c is, the rest must be made optimally, so best(a) = 1 + min over c of best(a − c). Lesson 8.5 listed every combination that sums to a target; here we only want the smallest, which turns an exponential search into a table of size amount.

ML context. Making an exact total from a fixed set of sizes appears in systems work: composing a global batch from the micro-batch sizes a kernel is tuned for (Change D), filling a memory budget with block sizes an allocator offers, or splitting a request across instance types. The greedy "biggest piece first" is tempting there too, and fails for the same reason.


1. Step 1: Clarify

That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson

Unlock "Coin Change"

The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.

Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee