3.7.14 · D3Algorithm Paradigms

Worked examples — DP problems — rod cutting, egg drop, DP on trees

2,517 words11 min readBack to topic

The scenario matrix

Every DP problem in this chapter is a table of subproblems. A "case class" is a region of that table that behaves differently. Here is every region we must cover:

# Case class What breaks / is special Covered by
A Degenerate input — size 0 The base case; recursion must stop here Ex 1
B Rod: normal max Loop over every leftmost cut Ex 2
C Rod: "no cut is best" Optimum = sell whole rod, all cuts lose Ex 3
D Egg: 1 egg (linear floor) No gambling allowed — forced scan Ex 4
E Egg: enough eggs (≈ binary search limit) Eggs plentiful → answer drops to Ex 5
F Egg: scarce eggs (2 eggs) The real "worst-case" trade-off Ex 6
G Tree: leaf / single node Base of the DFS; Ex 7
H Tree: include-vs-exclude conflict Heavy child forces a real choice Ex 8
I Greedy trap (word problem) Greedy loses, DP wins — the star tree Ex 9
J Exam twist — negative weights flip "always include?" Signs change the instinct Ex 10

We hit all ten cells with ten examples below. (Cell J appears exactly once, in Ex 10. The rod-cutting "sell-whole" surprise below is part of case C, not a separate twist.)


Group 1 — Rod Cutting cases (A, B, C)


Group 2 — Egg Drop cases (D, E, F)


Group 3 — DP on Trees cases (G, H, I, J)


Recall Quick self-test

Empty rod value ::: (max over empty set defined as ). Why can't 1-egg use binary search? ::: A broken egg is gone; you can't back up, so you must scan linearly, cost . Bridge from to ::: — fewest drops = first budget whose reach covers . recurrence ::: ; first with is the answer. Leaf node states in MWIS ::: (empty child-sum is 0). Why greedy fails on the star tree ::: Boss (10) blocks four reports; excluding boss yields .