Day 87 of 90MediumLeetCode

Unique Paths

Count the different routes from the top-left to the bottom-right corner of a grid when you may only move right or down.

Pattern
2D grid DP
Time box
25 min
Time
O(m · n)
Space
O(m · n)

Try it first — give it 25 minutes

0/4 revealed

Stuck? Take one hint at a time. The struggle is where the pattern sticks.

    The problem in plain English

    A robot sits in the top-left cell of a grid with m rows and n columns. Each move, it steps one cell right or one cell down, never left or up. Return how many different routes take it to the bottom-right cell.

    m and n are between 1 and 100, and the answer is guaranteed to fit in a 32-bit integer (at most 2 × 10⁹).

    Example: m = 3, n = 2 → 3. The routes are right-down-down, down-right-down and down-down-right.

    Start with brute force

    Walk every route recursively: from each cell, try going right and try going down, and count the routes that reach the corner.

    def paths(r, c):
        if r == m - 1 and c == n - 1: return 1
        if r >= m or c >= n: return 0
        return paths(r + 1, c) + paths(r, c + 1)

    It's correct, but it visits every route one by one. A 100 × 100 grid has more than 10⁵⁷ routes. Even an 18 × 18 grid has over 2 billion.

    Building the intuition

    The recursion keeps arriving at the same cells. Cell (1, 1) is reached by right-then-down and by down-then-right, and each time the recursion recounts every route from (1, 1) onwards. The grid only has m · n cells, so there are only m · n different questions.

    This is Climbing Stairs on a grid. On the stairs, step i was reached from i − 1 or i − 2. Here, a cell is reached from above or from the left. Walk through the recipe:

    1. State. dp[r][c] means the number of different routes from the top-left cell to cell (r, c).
    2. Recurrence. The last move into (r, c) was a down-move from (r − 1, c) or a right-move from (r, c − 1). Every route falls into exactly one of those groups. So dp[r][c] = dp[r − 1][c] + dp[r][c − 1].
    3. Base cases. Every cell in the top row can only be reached by moving right the whole way: 1 route. Every cell in the left column: only down, 1 route. That includes the start, dp[0][0] = 1.
    4. Order. Each cell reads the cell above and the cell to the left. Row by row, left to right, both are ready.

    The optimal approach

    1. Make an m × n table filled with 1. That sets the top row and left column at once.
    2. For r from 1 to m − 1, for c from 1 to n − 1: dp[r][c] = dp[r − 1][c] + dp[r][c − 1].
    3. Return dp[m − 1][n − 1].

    The subtle detail: why the edges are 1, not 0. It's tempting to say "the top row has nothing above it, so add 0 from above". That's the same thing, as long as you start with dp[0][0] = 1. Filling the edges with 1 directly avoids special cases inside the loop.

    Space optimisation. Row r only reads row r − 1. In fact one row is enough: keep row of length n, all 1s, and for each new row do row[c] += row[c − 1] from left to right. Before the update, row[c] still holds the value from above; row[c − 1] was just updated and holds the value from the left. Space drops to O(n).

    row = [1] * n
    for r in range(1, m):
        for c in range(1, n):
            row[c] += row[c - 1]   # above (old row[c]) + left (new row[c-1])
    return row[-1]

    The code

    Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.

    class Solution:
        def uniquePaths(self, m: int, n: int) -> int:
            # dp[r][c] = number of different paths from the top-left cell to cell (r, c)
            # every cell in the top row or left column can be reached exactly one way
            dp = [[1] * n for _ in range(m)]
    
            for r in range(1, m):
                for c in range(1, n):
                    # the last move into (r, c) came from above or from the left
                    dp[r][c] = dp[r - 1][c] + dp[r][c - 1]
    
            return dp[m - 1][n - 1]

    Dry run

    m = 3, n = 7. Row 0 and column 0 are the base cases (all 1). Every other cell is the one above plus the one to the left.

    r \ c0123456
    01111111
    11234567
    213610152128

    A few cells worked out:

    Cellabove + leftValue
    dp[1][1]dp[0][1] + dp[1][0] = 1 + 12
    dp[1][6]dp[0][6] + dp[1][5] = 1 + 67
    dp[2][3]dp[1][3] + dp[2][2] = 4 + 610
    dp[2][6]dp[1][6] + dp[2][5] = 7 + 2128

    The answer is 28.

    Complexity

    • Time: O(m · n). One addition per cell: at most 10,000 for a 100 × 100 grid.
    • Space: O(m · n) for the table, or O(n) with the single-row version.

    Edge cases and traps

    • A single row or column. m = 1 or n = 1 → 1 route. The loops don't run and the table is all 1s.
    • m = n = 1. Already at the goal: 1 route (doing nothing), not 0.
    • Initialising everything to 0. Then nothing ever becomes non-zero. The edges must be 1.
    • Mixing up rows and columns. The table is m rows by n columns; dp[m − 1][n − 1] is the corner. Swapping them gives the same number here, but it will bite you on Unique Paths II, where obstacles break the symmetry.

    Say it in the interview

    "Let dp[r][c] be the number of routes to cell (r, c). The last move came from above or from the left, so dp[r][c] = dp[r − 1][c] + dp[r][c − 1]. The top row and left column are all 1. I fill row by row in O(m·n) time. Since each row only needs the previous one, a single array with row[c] += row[c − 1] gives O(n) space. There's also a formula: choose which m − 1 of the m + n − 2 moves go down."

    Follow-ups interviewers ask

    • "Is there a closed form?" Yes. Every route is m − 1 downs and n − 1 rights in some order, so the answer is C(m + n − 2, m − 1). Compute it with a loop of multiplications and divisions in 64-bit to avoid overflow.
    • "Some cells are blocked." That's Unique Paths II: same recurrence, but a blocked cell is 0, and the edges stop being 1 after the first obstacle.
    • "Minimum cost path instead of counting?" Same grid and order, with dp[r][c] = cost[r][c] + min(above, left).

    Practise the same idea

    This is Day 87 of a free 90-day plan 🐸

    90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.

    Get the full roadmap — free