3.7.14 · D1Algorithm Paradigms

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

1,978 words9 min readBack to topic

This page assumes you have seen nothing. Before you can read the parent note, you must own every symbol it throws at you. We build them one at a time, each one earned before the next uses it.


0. What is an array, and what does mean?

An array is just a row of boxes, each holding one number, each labelled by a whole-number position called an index.

Figure — DP problems — rod cutting, egg drop, DP on trees
  • Picture: the red box in the figure is box number ; is whatever number lives there.
  • Why the topic needs it: rod cutting stores a price list where is the price of a rod-piece of length . Without indexing we could not say "the price of a length-3 piece" in symbols.

1. The notation and "for all"

  • Picture: a number line with a filled bar from to ; every tick inside is an allowed value of .
  • Why the topic needs it: every DP recurrence tries many choices — "the first cut could be length , or , ... up to ". The range says which choices are legal.

2. , , and — the three loops-in-disguise

These three symbols each mean "run through a range and combine the values". They are the beating heart of every formula in the parent.

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

3. Functions and the notation , ,

  • Picture: a box with arrows going in (inputs) and one arrow coming out (the answer).
  • Why one input vs. two vs. two-with-a-flag:
    • — rod revenue depends on one thing: the rod's length.
    • — egg-drop cost depends on two things: eggs left and floors left . Change either and you have a different subproblem.
    • / — tree value depends on the node and a yes/no flag ( = " excluded", = " included"). The flag exists because the parent's decision needs to know what the child decided.

4. Recursion and base cases — where the smaller copies come from

Figure — DP problems — rod cutting, egg drop, DP on trees
  • Picture: a staircase of shrinking problems; the bottom red step is the base case that returns an answer directly.
  • Why the topic needs it: without a base case the shrinking never stops. Every recurrence in the parent pairs a general rule with base cases — this is required before you meet Recursion and Memoization.

5. Overlapping subproblems and memoization

  • Why "bottom-up": if you fill in order, then when you compute every it needs is already done. No recursion needed — just an ordered fill.

6. Big-O notation , — the cost tag

  • Why the topic needs it: the whole point of DP is speed. Rod cutting is (for each of lengths, try up to first cuts). Tree DP is (each edge touched once). You cannot appreciate the win without this tag.

7. Trees, nodes, children, and DFS

Figure — DP problems — rod cutting, egg drop, DP on trees
  • Picture: the red node is the root; arrows point down to its children; the shaded blob is one subtree.
  • Why no cycles matters: because there are no loops, each subtree is a separate, independent subproblem — the exact condition DP needs. This connects to Tree Traversal (DFS).

The prerequisite map

Arrays and indexing p of i

Recurrence formulas

Range 1 to n

max min sum

Functions r of n

Base cases

Recursion

Overlapping subproblems

Memoization table

Big-O cost O of n squared

Trees nodes children

DFS post order

DP on trees

Rod cutting and Egg drop

3.7.14 DP problems

Everything on the left builds the machinery; it all funnels into the parent note DP problems. From here you are ready for Dynamic Programming, Recursion and Memoization, Knapsack Problem, and the contrasts with Greedy Algorithms and Binary Search.


Equipment checklist

Cover the right side and see if you can say it out loud.

What does mean, and how does index differ from value?
is the number stored in the box labelled ; the index is an address (e.g. "length"), the value is the content (e.g. "price").
What does describe?
The set of legal choices for : every whole number from up to and including .
In words, what do , , each do over a range?
= pick the biggest, = pick the smallest, = add them all up.
Why does egg drop need BOTH and ?
You over your drop-floor choice; nature -es to the worst-case outcome — it is a two-player move.
Why does have two inputs but has one?
The rod answer depends only on length; egg-drop cost depends on both eggs left and floors left — change either and it is a new subproblem.
What is a base case and why is it required?
A trivial input whose answer is known directly (e.g. ); without it, recursion shrinks forever and never returns.
State optimal substructure in one sentence.
The best solution to the whole is assembled from best solutions to its parts.
What problem does memoization solve?
Overlapping subproblems — it stores each subproblem's answer once so it is never recomputed, turning exponential into polynomial.
What does mean intuitively?
Work grows like squared; double the input and you do about four times the work.
Why must tree DP use post-order DFS?
A parent's value is built from its children's values, so every child must be finished before its parent is processed.
Why does a node need TWO states and ?
Because the parent's choice depends on whether was included; storing both lets the parent pick correctly.