Unique Paths
Unique Paths is LeetCode 62 (Medium). A robot starts in the top-left cell of an m × n grid and wants to reach the bottom-right cell. It can only move right or down. Return how many different routes it can take.
mis the number of rows andnthe number of columns, each at most 100.- The tests promise the answer is at most 2 × 10⁹, so it fits in a 32-bit
int.
- 1 <= m, n <= 100
- The answer is at most 2 × 10⁹
Intuition
Think about the last move into any cell. The robot either came down from the cell above or right from the cell on its left, and those two groups of routes never overlap. So:
routes to a cell = routes to the cell above + routes to the cell on the left
The top row and left column have only one route each, a straight line, because there is nowhere else to come from. Filling the grid row by row from that edge gives every inner cell after both its sources, and the bottom-right cell ends with the full count.
Only the previous row is ever read, so one array of n counts is enough: before a cell is updated it still holds the count from above, and its left neighbour already holds this row's value.
Counting routes through a grid where moves only go one way (right/down) is a fill-the-table DP: each cell's count is the sum of the counts of the cells that lead into it. Obstacles, costs or a triangle shape change the rule, not the pattern.
Approach
Before reading on, write the counts for a 3 × 3 grid by hand. Start with the top row and left column (how many ways reach those?), then fill each inner cell from its neighbours. Check the corner comes out 6.
Two ways to solve it
Add the count above and the count on the left, one row at a time.
- Adapts: blocked cells, costs and other move sets only change the update.
- No big intermediate numbers.
Every route is m − 1 downs and n − 1 rights in some order, so the answer is C(m + n − 2, m − 1).
- Fastest.
- Only for an empty grid; one blocked cell breaks it.
Both give the same number. The DP is what carries over to the follow-up questions, so the steps, code and live demo follow it. The formula comes after the demo.
Start with the top row: one route per cell
Make row = [1] * n. Every cell of the top row can only be reached by moving right along the row, so each has exactly one route.
Fill each later row in place
For each of the remaining m - 1 rows, leave row[0] at 1, since the left column is reached only by moving straight down. Then for j from 1 to n - 1, do row[j] += row[j - 1].
Why the in-place update adds the right two numbers
Just before row[j] changes, it still holds the count for the cell directly above. row[j - 1] was updated a moment earlier, so it is the cell on the left in the current row. Their sum is exactly the rule above-plus-left.
Read the corner
After the last row, row[n - 1] is the number of routes to the bottom-right cell.
Code in Python, C++ and Java
Combinations formula
A route is a string of m − 1 D moves and n − 1 R moves, and every ordering is a different valid route. So the count is the number of ways to choose which of the m + n − 2 moves are downs: C(m + n − 2, k) with k = min(m, n) − 1. Build it as a running product with big = max(m, n) − 1: after step i the value equals C(big + i, i), a whole number, so each division is exact. Intermediate products reach about 200 × 2 × 10⁹, so C++ and Java use 64 bits.
Common pitfalls
Counting m and n moves instead of m − 1 and n − 1
return comb(m + n, m)
return comb(m + n - 2, m - 1)
A grid with m rows needs only m − 1 steps down to go from the top row to the bottom row. For a 3 × 7 grid the wrong formula gives C(10, 3) = 120, while the real answer is C(8, 2) = 28.
Dividing before multiplying in the formula
ans *= (big + i) // i
ans = ans * (big + i) // i
The single fraction (big + i) / i is usually not a whole number. For a 3 × 3 grid, big = 2: step 1 gives 3 // 1 = 3, but step 2 gives 4 // 2 = 2 and the answer comes out 6 only by luck; for 3 × 4, step 2 is 5 // 2 = 2, giving 8 instead of 10.
Edge cases
m = 1, n = 5There is only one straight route. The DP never reaches an update (no second row, or no second column), and the formula's loop runs zero times, so both return 1.
Complexity
Unique Paths FAQ
What changes if some cells are blocked?
A blocked cell can't be part of any route, so its count is forced to 0 and the rest of the update stays the same. That version is Unique Paths II, and the combinations formula no longer works there.
Why is the 3 × 7 answer 28?
Every route is 2 downs and 6 rights, 8 moves in all. Choosing which 2 of the 8 moves go down gives C(8, 2) = 28, the same number the table puts in the corner.
Does swapping m and n change the answer?
No. Turning the grid on its side turns every right move into a down move and the other way round, so the routes match one for one. The formula shows it too: C(m + n − 2, m − 1) equals C(m + n − 2, n − 1).