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
- A robot starts at the top-left and may only move right or down. How many routes reach the bottom-right?
- Each cell can be reached from the cell above or the cell to the left.
- So paths(cell) = paths(above) + paths(left). The first row and column have exactly 1 path.
- Fill the grid row by row. O(rows × columns).
Watch it run
Loading animation…