A robot on an m × n grid starts at top-left and moves to bottom-right. It can only move right or down. Return the number of unique paths.
m = 3, n = 728m = 3, n = 36To reach (r,c) you must come from (r-1,c) or (r,c-1). So paths[r][c] = paths from above + paths from left. First row and column are 1 (one straight path). Fill top-to-bottom, left-to-right so dependencies are ready.
Exploring every path recursively visits the same cells many times. The number of paths explodes as the grid grows.
The robot can only enter a cell from above or from the left. So paths to a cell equals paths to the cell above plus paths to the cell on the left. Every cell in the first row or first column has exactly one path, a straight line. Fill the grid row by row, and the bottom right cell holds the answer.
Paths into a cell equal the sum of paths into the cells that lead to it. One row of memory is enough.
Run the animation above on your own input, or watch the 1:34 video explanation of Unique Paths with a dry run and Java code.