Unique Paths

Medium

Topics
MathDynamic ProgrammingCombinatorics

A robot sits at the top-left of an m x n grid and can only move right or down. Return the number of distinct paths to the bottom-right corner.

Example 1

0·······1·······2·······0123456
one route
Only moves right and down are allowed, so every route is a staircase from the start to the finish and all of them take the same number of steps. The shaded one is just an example; the answer counts how many such staircases exist, 28.
2D grid — one row per line.
Input:  m = 3, n = 7
Output: 28

Example 2

0·······1·······2·······0123456
one route
Input:  m = 3, n = 2
Output: 3

Constraints

  • 1 <= m, n <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

The number of ways to reach a cell is the ways to reach the cell above plus the cell to its left, because those are the only two moves. The whole top row and left column have exactly one path.

Approach

  1. Use a 1-D array row of length n filled with 1.
  2. For each subsequent row, update row[j] += row[j-1] left to right.
  3. The last entry is the answer.

Dry Run

m = 3 rows, n = 7 columns
One rolling row. It starts as the TOP row of the grid, where every cell has
exactly one path: straight along the top.
  row start      [1, 1, 1, 1, 1, 1, 1]
  after row 1    [1, 2, 3, 4, 5, 6, 7]
  after row 2    [1, 3, 6, 10, 15, 21, 28]
answer 28
The in-place update is doing something subtle. When row[j] += row[j-1] runs,
row[j] still holds the value from the row ABOVE and row[j-1] has already been
updated to this row's value -- so the one statement is exactly
"ways from above + ways from the left" without ever storing two rows.
That also explains why the inner loop starts at j = 1 rather than 0. Column 0 has
one path, straight down, and must stay 1 forever. Let j = 0 into the loop and
row[0] += row[-1] reads the LAST element of the row, wrapping around, and a 3x7
grid returns 43 instead of 28.
The numbers are binomial coefficients -- the final row is 1, 3, 6, 10, 15, 21, 28,
which is C(k+2, 2) -- so this can also be answered in closed form as
C(m+n-2, m-1) = C(8,2) = 28. The DP is the one to present when asked to generalise
to obstacles, which the closed form cannot handle.

Complexity

  • Time — O(m*n): fill every cell once.
  • Space — O(n): a single rolling row instead of the full grid.

Common Pitfalls

  • The inner loop starts at j = 1. Column 0 has exactly one path -- straight down -- and must stay 1; letting j = 0 into the loop makes it accumulate row[-1], the last element, so a 3x7 grid returns 43 instead of 28.
  • One row is enough because each cell only needs the value above it and the value to its left, and the left one is already the updated cell. That is also why the update has to be in place rather than into a fresh row.

Full Solution — all languages

class Solution:
    def uniquePaths(self, a, b):
        row=[1]*b
        for _ in range(1,a):
            for j in range(1,b):
                row[j]+=row[j-1]
        return row[-1]
Asked at Flipkart Ola Razorpay
SensAI
Beta
Tap the mic to stop voice mode