LeetCode 62 · Medium

Unique Paths Explained (LeetCode 62)

Updated Dec 2, 2026 · 3 min read

Every route into a cell comes from the cell above or the cell to the left, so ways[r][c] = ways[r-1][c] + ways[r][c-1], with every cell on the top row and left column set to 1. You only ever need the row above, so keep a single row and add each cell's left neighbor in place. That's O(m*n) time and O(n) space.

Unique Paths reel cover

Key points

  • ●Moves are only right or down, so a cell is entered from above or from the left.
  • ●The top row and left column each have exactly 1 route.
  • ●ways[r][c] = ways[r-1][c] + ways[r][c-1].
  • ●One row is enough: row[j] += row[j-1], repeated m - 1 times.
  • ●The answer is also a binomial: choose which m - 1 of the m + n - 2 moves go down.
Step-by-step visual sheet: Unique Paths
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: Leo's walk to the cafe

Leo stands in the top-left cell of a 3 by 3 block grid. The cafe is in the bottom-right cell. He only ever steps right or down, never back. How many different routes can he take?

Most people find two: all the way right then down, or all the way down then right. But the zigzags count too. Right, down, right, down works, and so do down, right, down, right, right, down, down, right, and down, right, right, down. That's 6 routes.

That's LeetCode 62, Unique Paths: given a grid with m rows and n columns, count the routes from the top-left cell to the bottom-right cell when every move is one step right or one step down.

Small tests first

Grid (rows x columns) Why Routes
1 x n just walk right 1
m x 1 just walk down 1
2 x 2 RD or DR 2
2 x 3 the one down step fits in 3 spots 3
3 x 3 2 rights and 2 downs in any order 6

If you guessed 4 for the 3 by 3, that's where guessing breaks.

The slow way: try every route

A plain recursion branches at every cell: go right or go down. Each level doubles the work, roughly 2^(m+n) calls. A 17 by 17 grid has 601,080,390 routes, and walking each one is far too slow.

Worse, the branches keep landing on the same cells. In a 3 by 3 grid the middle cell is reached twice. In a 17 by 17 grid, the center is reached 12,870 times, and each time the recursion recounts everything after it.

The idea: how many ways can I get here?

Flip the question. At every cell, ask how many routes reach it. Since moves are only right or down, a route arrives either from the cell above or from the cell on the left. Those two groups never overlap, so you add them:

ways[r][c] = ways[r-1][c] + ways[r][c-1]

The edges are easy. Along the top row you can only have walked right, and down the left column you can only have walked down, so every edge cell holds 1. The visual sheet above builds this rule in Step 3 and the edges in Step 4.

Trace it on the 3 by 3 grid

col 0 col 1 col 2
row 0 1 1 1
row 1 1 1 + 1 = 2 1 + 2 = 3
row 2 1 2 + 1 = 3 3 + 3 = 6

The corner reads 6, the same as the list of routes. Step 5 on the sheet fills this exact table.

The code in Python

Each row only needs the row above it. So keep one row and update it in place. Before the update, row[j] still holds the value from the row above, and row[j - 1] already holds the new value to the left:

def unique_paths(m, n):
    row = [1] * n
    for _ in range(m - 1):
        for j in range(1, n):
            row[j] += row[j - 1]
    return row[-1]

On a 3 by 3 grid the row goes [1, 1, 1], then [1, 2, 3], then [1, 3, 6], and it returns 6. On a 3 by 7 grid the last pass is [1, 3, 6, 10, 15, 21, 28], so it returns 28.

Complexity

  • Time: O(m*n), each cell is updated once.
  • Space: O(n), one row. The full 2D table on the sheet uses O(m*n), which also passes.

There's also a math shortcut. Every route is m + n - 2 moves, and you only choose which m - 1 of them go down, so the answer is C(m+n-2, m-1). For the 3 by 3 grid that's C(4,2) = 6.

The trap to watch for

The inner loop starts at index 1, not 0. The first cell of every row is an edge cell and must stay 1. Start at 0 and row[-1] sneaks in as the "left neighbor", which wraps around to the end of the list and breaks every count.

Frequently asked questions

Why does a 3 by 3 grid have 6 unique paths?

Every route is 2 rights and 2 downs in some order: RRDD, RDRD, RDDR, DRRD, DRDR and DDRR. Most people only find the two that hug the edges. The table finds all of them: the bottom-right cell ends at 3 + 3 = 6.

What is the time complexity of Unique Paths?

The table version fills every cell once, so it's O(m*n) time. With a single row it uses O(n) space. Trying every route instead grows exponentially, about 2^(m+n).

Can Unique Paths be solved with math?

Yes. A route is m + n - 2 moves, and you pick which m - 1 of them go down, so the answer is C(m+n-2, m-1). For a 3 by 3 grid that's C(4,2) = 6, and for 3 by 7 it's C(8,2) = 28.

How is Unique Paths like Climbing Stairs?

Both add two earlier answers. Climbing Stairs adds one step back and two steps back on a line. Unique Paths adds the cell above and the cell to the left on a grid.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free