InterviewLegend

Grid Dynamic Programming

Alex Chen30 min readMedium

Every grid DP problem hangs on one fact: a cell's answer depends only on the cell above it and the cell to its left. Fill the table row by row, left to right, and the answer is sitting in the far corner when you finish. Unique Paths is the bare skeleton of the pattern. Get it into your hands and obstacles, minimum-cost paths, and largest-square all turn out to be the same loop with the arithmetic swapped out.

Grid DP Pattern: State is (row, col). Each cell depends on cells above and/or to the left. Fill row by row, left to right.

dp[r][c] = answer for reaching cell (r, c) from origin
What's inside
  • 1 problem, worked step by step
  • about 30 min

Premium lessonData Structures & Algorithms

Keep going with Premium.

All 78 lessons and 103 worked problems in this course, plus what 100 companies ask, pulled from 20,000+ candidate reports.

100 companies. Checked every month.

Unlock the full course

$49 a year · less than $1 a week · 30-day money-back guarantee