Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Dynamic programming

Grid DP: cells filled from their neighbours

Lesson · 9 min read · Dynamic programming · Pattern: grid DP

When moves only go right and down, each cell's answer comes from the cell above and the cell to the left. Fill row by row.

The pattern

On a grid where movement only goes right and down (or the shape being measured grows only one way), a cell's answer depends on a fixed set of neighbours that are already filled if you go row by row:

Grid DP

dp[r][c]

  1. = cell value ⊕ opt(dp[r − 1][c], dp[r][c − 1]) for paths;
  2. = 1 + min(up, left, up-left) for the largest square ending here.

boundary

  1. the first row and column, or an extra row and column of identity values.

When the grid allows moves in all four directions, the dependency graph has cycles and this pattern does not apply: that is a shortest-path problem (Dijkstra or breadth-first search).

Worked example: the minimum path sum

Cell costs [[1,3,1,2],[1,5,1,3],[4,2,1,1]]; moves right or down.

  1. Step 1 / 12
    01230112

    Base cases filled first.

  2. Step 2 / 12
    012301412

    (0, 1): 3 + min(—, 1) = 4.

  3. Step 3 / 12
    0123014512

    (0, 2): 1 + min(—, 4) = 5.

  4. Step 4 / 12
    01230145712

    (0, 3): 2 + min(—, 5) = 7.

  5. Step 5 / 12
    012301457122

    (1, 0): 1 + min(1, —) = 2.

  6. Step 6 / 12
    0123014571272

    (1, 1): 5 + min(4, 2) = 7.

  7. Step 7 / 12
    01230145712762

    (1, 2): 1 + min(5, 7) = 6.

  8. Step 8 / 12
    012301457127692

    (1, 3): 3 + min(7, 6) = 9.

  9. Step 9 / 12
    0123014571276926

    (2, 0): 4 + min(2, —) = 6.

  10. Step 10 / 12
    01230145712769268

    (2, 1): 2 + min(7, 6) = 8.

  11. Step 11 / 12
    012301457127692687

    (2, 2): 1 + min(6, 8) = 7.

  12. Step 12 / 12
    0123014571276926878

    (2, 3): 1 + min(9, 7) = 8. The cheapest path costs 8.

Variations

  • Obstacles: a blocked cell contributes 0 ways.
  • Largest square of 1s: the square ending at (r, c) is limited by the three squares ending above, left and diagonally.
  • Dungeon: when health must stay positive, fill from the target backwards, because the requirement at a cell depends on what comes after.
TimeO(rows · cols)
SpaceO(cols) keeping one row

The problems in this module: