Skip to content
StackDecode
Dynamic programmingAnimated

Unique Paths

Paths in a grid moving right or down = paths from above + paths from the left.

Time

O(m · n)

Extra space

O(n)

Use it for

Counting grid routes.

How it works

  1. A robot starts at the top-left and may only move right or down. How many routes reach the bottom-right?
  2. Each cell can be reached from the cell above or the cell to the left.
  3. So paths(cell) = paths(above) + paths(left). The first row and column have exactly 1 path.
  4. Fill the grid row by row. O(rows × columns).

Watch it run

Loading animation…