Math Fundamentals18 sections · 814 units
Open in Course

Unique Paths - The Formula

(Sum of incoming paths)

To reach cell (i,j)(i, j), you came from either (i1,j)(i-1, j) (from above) or (i,j1)(i, j-1) (from left).

The number of paths to (i,j)(i, j) is the sum of paths to those two cells: dp[i][j]=dp[i1][j]+dp[i][j1]dp[i][j] = dp[i-1][j] + dp[i][j-1].

Base case: The top row and left column each have exactly 1 path (you can only move in one direction).