3.7.14 · D4Algorithm Paradigms

Exercises — DP problems — rod cutting, egg drop, DP on trees

2,771 words13 min readBack to topic

This page is a self-test. Read the problem, try it yourself, then open the collapsible solution. Every problem builds on the parent note and the core idea of Dynamic Programming: find the recursive structure, then stop recomputing it.

Every recurrence here rests on three prerequisites you should keep open: Recursion and Memoization (why solving once and storing wins), Tree Traversal (DFS) (the order we visit tree nodes), and Binary Search (which shows up as a wrong shortcut and a right optimization).


Level 1 — Recognition

Exercise 1.1 — Name the subproblem

Recall Solution

This is pure pattern recognition — no arithmetic.

  • (a) a shorter rod. We cut once at the left, earn , and are left with a rod of length the same problem, smaller input.
  • (b) a smaller (eggs, floors) pair. After one drop, either the egg broke (fewer eggs, fewer floors below) or survived (same eggs, fewer floors above). Either way the state shrinks.
  • (c) a subtree. Because a tree has no cycles, each child's subtree is a self-contained problem whose answer we combine at the parent.

The single unifying sentence: "What is the smallest set of subproblems whose answers, combined, give the answer to the whole?"

Exercise 1.2 — Read the base case

Recall Solution
  • . With zero floors there is nothing to test — the threshold question is already answered, so we make zero drops. (Number of eggs is irrelevant when there is no floor.)
  • . With a single egg, a break is permanent loss of information. If you gamble and drop from floor and it breaks, you cannot test floors anymore. So the only safe strategy is to climb floor by floor from the bottom, worst case drops.

Level 2 — Application

Exercise 2.1 — Turn the rod-cutting crank

Recall Solution

WHAT we do: fill the table left to right, each entry trying every leftmost piece length .

computation first cut
0 0
1 2 1
2 5 2
3 7 1
4 10 2

WHY lands on 10: taking leftmost piece (price 5) leaves a length-2 rod worth , total — beating selling whole at .

Reconstruct: first cut at is → piece of length 2. Remaining rod length ; first cut at is → another length-2 piece. Pieces , revenue .

Exercise 2.2 — Turn the tree crank

Recall Solution

WHAT we do: process leaves first (post-order DFS), because a parent needs its children's answers.

  • Node 3 (leaf): , .
  • Node 2 (child 3): . .
  • Node 1 (child 2): . .
  • Node 0 (child 1): . .

Answer .

WHY 8: excluding node 0 (state ) lets node 1's subtree do its best (which was to exclude 1 and grab node 2). Including node 0 forces excluding node 1, but node 2 is now free — set with weight wins.


Level 3 — Analysis

Exercise 3.1 — Compare choices in egg drop

Recall Solution

For each candidate first-drop floor , the two branches are: break → , survive → . We take their (adversary), add 1, then pick the giving the smallest total.

break survive
1 0 3 4
2 1 3 4
3 2 2 3
4 3 3 4
5 4 4 5
6 5 5 6

The minimum is at : , optimal first drop from floor 3.

WHY balances the two branches best: a good first drop equalizes "cost if it breaks" and "cost if it survives". At both branches cost , so neither the break nor the survive case can be worse than the other — the adversary has no lever to pull.

Exercise 3.2 — Reconstruct the rod cut path

Recall Solution

Reconstruction walks the cut array like a linked list:

  • At length : → emit a piece of length . Remaining .
  • At length : → emit a piece of length . Remaining .
  • Length → stop.

Pieces , revenue . This matches , confirming the reconstruction.


Level 4 — Synthesis

Exercise 4.1 — Invent a recurrence: rod cutting with a per-cut cost

Recall Solution

WHAT changes: charging a cut whenever we split. Model it by paying each time we take a leftmost piece and there is a rod left over.

Let = best net revenue for length . Trying leftmost length :

  • if we sell the whole thing, no cut: value ;
  • if we make one cut (fee ) and continue: value .

Apply ():

  • .
  • .
  • .
  • .

WHY the answer dropped from 10 to 9: the old winner earned gross but needs one cut, netting — now merely tied with selling the whole rod at (no cut). The cut fee erased the advantage of splitting.

Exercise 4.2 — Invent states: tree with "at most one child kept"

Recall Solution

WHAT constraint we face: unlike MWIS (no adjacent pair), here a picked parent is allowed one picked child. So the parent's value depends on how many children it "spends" its allowance on.

Define two states per node :

  • = best in subtree of with not picked — children unconstrained.
  • = best in subtree of with picked — at most one child may also be picked.

not picked: every child chooses its own best, no coupling:

picked: the default is each child takes (not picked). But we're allowed to "upgrade" one child to picked. Upgrading child gains if that's positive. Pick the single most profitable upgrade:

g(v,1)=\text{base}+\max\Big(0,\ \max_{c}\big(g(c,1)-g(c,0)\big)\Big).$$ Answer $=\max\big(g(\text{root},0),\,g(\text{root},1)\big)$; still $O(n)$ since each edge is touched once. WHY correct: subtrees are disjoint so values add; the only coupling is the "one upgrade" allowance, captured by choosing the best single child gain.

Level 5 — Mastery

Exercise 5.1 — The dual "forward" egg-drop and a complexity claim

Recall Solution

WHAT the dual says: instead of "min drops for floors", ask "with drops, how many floors can I clear?" Grow until .

For eggs, and (one egg clears floors in drops). This telescopes to the triangular numbers .

12 78
13 91
14 105 ≥ 100 ✓

Smallest with is .

WHY it's faster: the naive table fills cells, each doing an inner scan → . The forward version fills an table with per cell, and is only for small — here versus . So we do constant-time steps instead of .

Exercise 5.2 — Why greedy fails on a star, exactly

Recall Solution

(a) Greedy: takes the centre (weight , the single heaviest). The centre is adjacent to all leaves, so every leaf is now forbidden. Greedy total .

(b) DP optimum via the two MWIS states, root = centre:

  • Each leaf : , .
  • Centre included: .
  • Centre excluded: .
  • Answer → set = the four leaves.

(c) When greedy loses: whenever a heavy node's neighbours collectively out-weigh it. Formally, picking node is a mistake if . Greedy is locally right ( per node) but globally wrong (). The two-state DP compares include-vs-exclude across the whole subtree at once, so it never falls for this. This is the same reason Greedy Algorithms fail on the Knapsack Problem: a locally best pick can block a globally better combination.


Recall Self-check: match problem → state definition

Rod cutting state ::: = best revenue from a rod of length (one dimension). Egg drop state ::: = min worst-case drops with eggs, floors (two dimensions). Tree MWIS states ::: and = best subtree weight with excluded / included. Egg drop dual ::: = max floors clearable with eggs and drops.