solulubeta
Sign in
Recursion & Memo / DP patterns for coding tests
04.02 · Lesson

DP on Grids

Count paths or find the cheapest path in a grid. The state is the cell, the choices are the moves, and the table fills row by row.

9 min 1 video Quiz Pro

A robot starts at the top-left cell and moves only right or down. The state is simply the cell you are on.

Count the paths

Look at the last move into cell (r, c). It came from above (r − 1, c) or from the left (r, c − 1).

  • Promise: f(r, c) = number of paths from (0, 0) to (r, c).
  • Base cases: outside the grid → 0. The start (0, 0) → 1.
  • Recurrence: f(r, c) = f(r − 1, c) + f(r, c − 1).
  • Memo: int[R][C]. Time O(R · C).
solulu Pro

Continue this lesson with Pro

Inside: 5 sections · 1 video · quiz.

  • Count the paths
  • Blocked cells
  • Cheapest path instead
  • As a table
  • Derive it yourself
Lifetime ₹1,999 ₹999 · Launch offer
Mark this as done when you are finished.

Item 28 of 44 · updated 2026-10-13