LeetCode #62 Medium

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.

  • m is the number of rows and n the 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.
Constraints
  • 1 <= m, n <= 100
  • The answer is at most 2 × 10⁹
dpmathcombinatorics
Open on LeetCode ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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.

1

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.

2

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].

3

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.

4

Read the corner

After the last row, row[n - 1] is the number of routes to the bottom-right cell.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def uniquePaths(self, m: int, n: int) -> int:
▶3 row = [1] * n
▶4 for _ in range(1, m):
▶5 for j in range(1, n):
▶6 row[j] += row[j - 1]
▶7 return row[n - 1]
routes to each cell1111111from above + from the left+=top row: 1 route to each cell
row1 1 1 1 1 1 1
A cell in the top row can only be reached by moving right along that row, never from above, so each has exactly one route. This row of 1s is the array the code starts with.
routes to each cell11111111from above + from the left+=row 2: left edge keeps 1 route
row2
The first cell of row 2 has nothing on its left, so the robot can only have come straight down. It keeps 1 route, which is why the loop starts at the second column.
routes to each cell111111112from above + from the left1+1=21 from above, 1 from the left → 2 routes
cellrow 2, col 2
above1
left1
routes2
The last move into this cell was either down from above (1 route) or right from the left (1 route). No route can do both, so the counts add to 2.
routes to each cell1111111123from above + from the left1+2=31 from above, 2 from the left → 3 routes
cellrow 2, col 3
above1
left2
routes3
The last move into this cell was either down from above (1 route) or right from the left (2 routes). No route can do both, so the counts add to 3.
routes to each cell11111111234from above + from the left1+3=41 from above, 3 from the left → 4 routes
cellrow 2, col 4
above1
left3
routes4
The last move into this cell was either down from above (1 route) or right from the left (3 routes). No route can do both, so the counts add to 4.
routes to each cell111111112345from above + from the left1+4=51 from above, 4 from the left → 5 routes
cellrow 2, col 5
above1
left4
routes5
The last move into this cell was either down from above (1 route) or right from the left (4 routes). No route can do both, so the counts add to 5.
routes to each cell1111111123456from above + from the left1+5=61 from above, 5 from the left → 6 routes
cellrow 2, col 6
above1
left5
routes6
The last move into this cell was either down from above (1 route) or right from the left (5 routes). No route can do both, so the counts add to 6.
routes to each cell11111111234567from above + from the left1+6=71 from above, 6 from the left → 7 routes
cellrow 2, col 7
above1
left6
routes7
The last move into this cell was either down from above (1 route) or right from the left (6 routes). No route can do both, so the counts add to 7.
routes to each cell111111112345671from above + from the left+=row 3: left edge keeps 1 route
row3
The first cell of row 3 has nothing on its left, so the robot can only have come straight down. It keeps 1 route, which is why the loop starts at the second column.
routes to each cell1111111123456713from above + from the left2+1=32 from above, 1 from the left → 3 routes
cellrow 3, col 2
above2
left1
routes3
The last move into this cell was either down from above (2 routes) or right from the left (1 route). No route can do both, so the counts add to 3.
routes to each cell11111111234567136from above + from the left3+3=63 from above, 3 from the left → 6 routes
cellrow 3, col 3
above3
left3
routes6
The last move into this cell was either down from above (3 routes) or right from the left (3 routes). No route can do both, so the counts add to 6.
routes to each cell1111111123456713610from above + from the left4+6=104 from above, 6 from the left → 10 routes
cellrow 3, col 4
above4
left6
routes10
The last move into this cell was either down from above (4 routes) or right from the left (6 routes). No route can do both, so the counts add to 10.
routes to each cell111111112345671361015from above + from the left5+10=155 from above, 10 from the left → 15 routes
cellrow 3, col 5
above5
left10
routes15
The last move into this cell was either down from above (5 routes) or right from the left (10 routes). No route can do both, so the counts add to 15.
routes to each cell11111111234567136101521from above + from the left6+15=216 from above, 15 from the left → 21 routes
cellrow 3, col 6
above6
left15
routes21
The last move into this cell was either down from above (6 routes) or right from the left (15 routes). No route can do both, so the counts add to 21.
routes to each cell1111111123456713610152128from above + from the left7+21=287 from above, 21 from the left → 28 routes
cellrow 3, col 7
above7
left21
routes28
The last move into this cell was either down from above (7 routes) or right from the left (21 routes). No route can do both, so the counts add to 28. This is the bottom-right cell.
routes to each cell1111111123456713610152128from above + from the left+=28 routes to the corner
answer28
Every cell was filled after both cells that feed it, so the corner's count is final: 28 routes. As a check, that is the number of ways to order 2 downs among 8 moves.
05

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.

▶1class Solution:
▶2 def uniquePaths(self, m: int, n: int) -> int:
▶3 big, k = max(m, n) - 1, min(m, n) - 1
▶4 ans = 1
▶5 for i in range(1, k + 1):
▶6 ans = ans * (big + i) // i
▶7 return ans
06

Common pitfalls

Counting m and n moves instead of m − 1 and n − 1

✗ Wrong
return comb(m + n, m)
✓ Right
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

✗ Wrong
ans *= (big + i) // i
✓ Right
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.

07

Edge cases

A single row or column, such as m = 1, n = 5

There 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.

08

Complexity

Time
O(m · n)
Space
O(n)
Each of the (m − 1) × (n − 1) inner cells costs one addition. Only one row of n counts is stored; passing the smaller of m and n as the row length makes it O(min(m, n)).
09

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).