Unit 5 of 9 · about 4 hours · derive, implement

Grid, path and interval dynamic programming

Iteration direction follows the dependency direction. Survival predicates read successors, so the loop runs backward.

You'll be able to

  • Derive forward fills for counting and min-sum grids and backward fills for survival predicates.
  • Derive interval recurrences that try the last operation in and fill by increasing length.
  • Implement both families with brute-force differential tests on small grids and intervals.

Covers

Grid, path and interval dynamic programming
1

Read

In this order, only the parts listed

0 of 3 done

  1. Forward fills and the backward twistDSA Handbook - dynamic programming

    The pattern on LC 62 ( worked example, answer 28) and The twist on LC 174 (backward recurrence with the clamp and reversed loops)

  2. Interval DP with the last operationDSA Handbook - dynamic programming

    The pattern: tries the LAST operation in the range; Worked example: ; What CLRS calls matrix-chain multiplication

    The chapter labels LC 1039 as Matrix Chain Multiplication; on LeetCode it is Minimum Score Triangulation of Polygon, which has the same interval shape.

  3. Interval order on optimal search treesAlgorithms - dynamic programming

    Section 3.9 (pp. 117-119): interval recurrence and its three valid evaluation orders: by diagonal, row by row from the bottom up, and column by column from left to right

2

Focus on

  • Forward DP fails Dungeon Game because HP-needed-at-entry depends on the suffix, not the prefix. Backward state is HP needed on entering .
  • The clamp is the survival predicate per cell, not a guard. Dropping it admits zero-HP entries the knight cannot use.
  • Interval cells depend on strictly shorter intervals, so length-ascending fill is the order. Any single direction that respects shorter-first works.
Common mistakes · 3
  • Copying the LC 64 idea into Dungeon Game: maximize the path sum, then read the HP it needs. On the problem's own example the max-sum path (down, down, right, right; sum 28) needs 8 HP, but right, right, down, down needs 7.
  • Continuing the LC 63 base row past the first obstacle. Cells past a top-row wall are unreachable; the base loop must break, not continue.
  • Filling interval tables row-major left to right. Cell reads and , which a plain row-major pass may not have written; iterate by length.
3

Try it yourself

Closed-book, before checking anything

Closed-book in Python 3.13: unique_paths(3, 7) = 28, min_path_sum([[1,3,1],[1,5,1],[4,2,1]]) = 7, dungeon([[-2,-3,3],[-5,-10,1],[10,30,-5]]) = 7, burst([3,1,5,8]) = 167. State the grid sizes and all four expected outputs.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Why does Dungeon Game iterate bottom-right to top-left?Answer

    reads and . Reversed row-major order keeps both reads on computed cells; forward order reads sentinels.

  2. 2.Unique Paths is 28. Name the closed form and when it breaks.Answer

    . It breaks with obstacles, weights or backward predicates; the DP survives all three.

  3. 3.Why does Burst Balloons try the last burst in instead of the first?Answer

    Bursting first changes the neighbors of the rest, so subintervals are not independent. Bursting last fixes its neighbors to the interval boundaries, which splits into independent and .

  4. 4.A forward fill keeps, per cell, the smallest starting HP needed so far. Why can it fail?Answer

    A predecessor that needed less HP so far may arrive with less HP left, and one number cannot keep both. On [[-2,-1,-1],[-3,-1,0],[2,0,-3]] the forward fill answers 8 while the backward fill gives 7.

5

Practice

  • LeetCode 62 - Unique Paths

    Check that a 3 by 7 grid gives 28 paths and a 3 by 2 grid gives 3, then submit.

    Fill row-major and state the read set above the loop. After submitting, compare with the DSA Handbook grid-dp LC 62 section.

  • LeetCode 174 - Dungeon Game

    Check [[-2,-3,3],[-5,-10,1],[10,30,-5]] = 7 and [[0]] = 1, then submit.

    Iterate backward with the clamp. After submitting, compare with the DSA Handbook grid-dp LC 174 section and the AlgoMaster Dungeon Game lesson (https://algomaster.io/learn/dsa/dungeon-game).

You're done when

The four reproduce numbers come out of a blank file, and you can state each recurrence read set and loop direction.

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.