Unit 1 of 9 · about 3 hours · derive

Dynamic programming states, transitions and base cases

Every later card asks for the state first. If the state misses information the future needs, no memo or loop order can fix it.

You'll be able to

  • Write the state, recurrence, base cases and evaluation order for an unseen optimization problem before coding.
  • Explain why optimal substructure and overlapping subproblems are both needed, using a negative example.
  • Prove a recurrence considers every feasible optimum and never admits an invalid solution.

Covers

State sufficiency and overlapping subproblemsOptimal substructure and transition completenessBase cases, invalid states and initialization
1

Read

In this order, only the parts listed

0 of 4 done

  1. Smart recursion first, tables secondAlgorithms - dynamic programming

    Section 3.4, The Pattern: Smart Recursion (pp. 105-107): specification plus solution, then subproblems, dependencies and evaluation order

  2. Text segmentation recurrenceAlgorithms - dynamic programming

    Section 3.3 (p. 105): recurrence with base and IsWord calls

  3. Optimal substructure by contradictionAlgorithms - dynamic programming

    Section 3.7 Edit Distance, Recursive Structure (pp. 111-112): remove the last column of an optimal alignment, and the rest is optimal for the prefixes, proved by contradiction

  4. The two conditions with a negative exampleDSA Handbook - dynamic programming

    What dynamic programming actually is: optimal substructure plus overlapping subproblems, with merge sort as the negative example

    The opening says naive fib(43) makes about 433 million calls; that is , the return value. Erickson Section 3.1 gives the call count , about 1.4 billion for .

2

Focus on

  • A state is sufficient when the future depends only on the state, never on how it was reached. needs only the suffix start .
  • Optimal substructure is a claim about subproblems: removing the last column of an optimal alignment leaves an optimal alignment of the shorter prefixes.
  • Base cases define the empty input. and are load-bearing, not boilerplate.
  • Merge sort has optimal substructure without overlapping subproblems, so a memo never hits. Both conditions must hold.
Common mistakes · 3
  • Adding a state dimension for safety. Extra dimensions multiply the table for no gain; keep the smallest sufficient state.
  • A state that forgets a constraint the future needs. On a circle of houses (LC 213), over the index alone can rob both the first and the last house; run the line twice, without the first house and without the last, or add "first house robbed" to the state.
  • Writing the table before the recurrence. Without a one-sentence English specification of the subproblem you cannot tell a wrong recurrence from a wrong loop order.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write the recurrence with its base case, then run it in Python 3.13 on () with the word set ARTIST, OIL, ART, IS, TOIL: it returns True. Change OR to a sum to count segmentations: 2 (ARTIST.OIL and ART.IS.TOIL).
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why is the correct base case?Answer

    An empty suffix needs no words, so it segments trivially. The recurrence checks for , which reads exactly this cell.

  2. 2.Merge sort splits, recurses and combines. Why is it not dynamic programming?Answer

    Its halves share no subproblems, so a memo never hits. Optimal substructure alone does not make DP; overlapping subproblems must also hold.

  3. 3.How does the edit-distance proof show three predecessors are enough?Answer

    By contradiction on the last alignment column: it must be an insertion, a deletion or a substitution. If the remaining prefixes had a shorter edit sequence, gluing the last column back would shorten the whole, contradicting optimality.

  4. 4.Your recurrence passes the samples but you cannot state its subproblem in one English sentence. What do you do?Answer

    Stop and write the specification first. Without it a wrong recurrence and a wrong loop order look identical in the test output.

5

Practice

  • LeetCode 70 - Climbing Stairs

    Solve (answer 2) and (answer 8) by hand from with , then submit.

    Write the state, transition and base cases on paper first. After submitting, compare with the DSA Handbook dp-1d Climbing Stairs section.

  • LeetCode 198 - House Robber

    Derive and test [2,1,1,2] (answer 4) before submitting.

    The alternating parity pick returns 3 on [2,1,1,2] and is wrong. After submitting, read the AlgoMaster House Robber lesson (https://algomaster.io/learn/dsa/house-robber).

You're done when

You write state, recurrence, base cases and order for Climbing Stairs and House Robber without notes, and can show why the parity-greedy pick fails on [2,1,1,2].

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 6 more in the section
  • DSA Handbook - dynamic programmingHandbook Academy · primary

    Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.

  • Algorithms - dynamic programmingJeff Erickson · required

    Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.

Also in this section

  • AlgoMaster DSA - dynamic programming sliceAlgoMaster · required

    Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.

  • Hypothesis - exhaustive and differential DP testsHypothesis · support

    Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.

  • NeetCode - unseen mixed problemsNeetCode · optional

    Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.

  • Python sys: recursion limitPython Software Foundation · reference

    Read sys.getrecursionlimit and sys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default with getrecursionlimit (1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs.

  • Erickson lecture note D - Advanced Dynamic ProgrammingJeff Erickson · reference

    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in space.

  • Erickson lecture note J - Approximation AlgorithmsJeff Erickson · reference

    Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the GreedyLoadBalance heuristic, and Theorem J.1 with its factor-2 proof.