Dynamic Programming, Proof & Transfer
Dynamic programming tests whether a problem state contains exactly the information the future needs. Deriving that state and proving the recurrence transfers directly to batching, caching and resource allocation.
Section 5 of 5 · 9 units · about 31 hours
By the end you can Derive and implement two tag-hidden dynamic programs from blank files, reconstruct a witness, and repair one subtly wrong recurrence using a minimized counterexample.
Units
- Dynamic programming states, transitions and base cases3h
- Memoization, tabulation and dependency order3h
- Solution reconstruction and rolling state3h
- Sequence and string dynamic programming4h
- Grid, path and interval dynamic programming4h
- Tree dynamic programming3h
- Knapsack, subset and bitmask dynamic programming4h
- Recurrence proofs and differential validation3h
- Dynamic programming as shortest paths, formulations and limits4h
Build it
Mini project · about 10 to 14 hours
Two tag-hidden schedules with witnesses and a repaired recurrence
Derive two scheduling dynamic programs from blank files, each returning a witness for an independent checker. A differential harness shrinks, pins and repairs one subtly wrong recurrence, training the formulation judgment the capstone needs.
- 1Solve LC 983 Minimum Cost For Tickets with tags hidden: write the subproblem specification, recurrence, base case and evaluation order before code. Return the cost with a witness of the passes bought.
- 2Build the harness: Hypothesis generates small inputs and compares memoized, tabulated and brute-force answers, while a checker validates that each witness covers every travel day at exactly the reported total.
- 3Plant an off-by-one in the pass coverage, let Hypothesis shrink a failure, pin it with
@exampleand repair the recurrence. Map the shrunk input to the exact claim the planted recurrence made. - 4Solve LC 1335 Minimum Difficulty of a Job Schedule with tags hidden, with a state over the first unscheduled job and days left that returns its cut points as a witness. Differential-test against brute force over all cut positions on small inputs.
- 5Prove both recurrences by induction: name the case split, why the state is sufficient, and why no invalid schedule is admitted.
- 6Write a transfer note for each problem: the smallest sufficient state and one smaller state that fails, with a counterexample.
What you hand in
- Both solutions with specifications, witnesses and an independent witness checker.
- The Hypothesis harness with the pinned failure, the planted recurrence and its repair.
- The proofs and transfer notes for both problems.
How to check it
- Self-grade with the rubric below
- LeetCode 983: Minimum Cost For Tickets
Submit the repaired recurrence.
- LeetCode 1335: Minimum Difficulty of a Job Schedule
Submit after the brute-force comparison passes.
How it's graded
Recurrence formulation
Pass: Specification, recurrence, base cases and order are written before code for both problems.
Great: Each transfer note refutes a smaller state with a concrete counterexample.
Witness reconstruction
Pass: Both solutions return witnesses that an independent checker validates.
Great: The witness checker runs inside the Hypothesis harness on every example.
Harness and repair
Pass: Memoized, tabulated and brute-force answers agree on generated examples, while the planted recurrence fails with its shrunk input pinned and the repair passing.
Great: The write-up maps the shrunk input to the exact claim the planted recurrence made.
Inductive proof
Pass: One recurrence is proved by induction with its case split named.
Great: Both are proved, including why no invalid schedule is admitted.
Resources
- DSA Handbook - dynamic programmingHandbook Academy
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.
- AlgoMaster DSA - dynamic programming sliceAlgoMaster
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.
- Algorithms - dynamic programmingJeff Erickson
Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.
- Hypothesis - exhaustive and differential DP testsHypothesis
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
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
Read
sys.getrecursionlimitandsys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default withgetrecursionlimit(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
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
Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the
GreedyLoadBalanceheuristic, and Theorem J.1 with its factor-2 proof.