Unique Paths – Solution & Complexity
1. Understand the Pattern
- The robot only moves right or down, so every path is a sequence of moves.
- Reaching cell
(i, j)is only possible from(i-1, j)(a down move) or(i, j-1)(a right move). - The number of ways to reach a cell is the sum of the ways to reach those two neighbours.
2. Dynamic Programming Table
- Build an
m x ntable wheredp[i][j]is the number of paths to that cell. - The first row and first column are all
1(only one straight-line path). - Every other cell is
dp[i-1][j] + dp[i][j-1].
3. Optimise the Space
- Each row only depends on the row above, so a single 1-D array suffices.
- Iterate rows top to bottom, adding the value to its left in place.
- This drops memory to
O(n).
4. Closed-Form Solution and Complexity
- Any path makes exactly
m - 1down moves andn - 1right moves. - The count is the binomial coefficient
C(m + n - 2, m - 1). - Computing it multiplicatively is
O(min(m, n))time andO(1)space.