0

Grid DP

dp[r][c] depends on the cells you can arrive from, usually up and left. Walk the grid in dependency order.

Dynamic ProgrammingO(rows * cols)New13 problems
18
mastery

Trigger phrases

If a problem statement sounds like one of these, try this pattern first.

unique paths
minimum path sum
maximal square
dungeon game

The template

Get this into muscle memory, then adapt the condition per problem.

for (let r = 0; r < R; r++)
  for (let c = 0; c < C; c++)
    dp[r][c] = grid[r][c] + Math.min(r ? dp[r-1][c] : INF, c ? dp[r][c-1] : INF);
Watch Minimum Path Sum

If you are stuck

The generic hint ladder for this pattern, in order.

  1. 1Movement is restricted, which fixes the dependency direction.
  2. 2Initialise the first row and column separately.
  3. 3Some variants must be filled from the bottom right backwards.

Drill it

Every problem in the bank that uses this pattern.

0 / 13 solved
1
MDynamic Programming
2
3
MDynamic Programming
4
MGraphs
5
MDynamic Programming
6
7
MDynamic Programming
8
MDynamic Programming
9
HDynamic Programming
10
11
HDynamic Programming
12
13
MDynamic Programming

Other patterns in Dynamic Programming