62. Unique Paths - Detailed Explanation
Problem statement
You are given two integers, m and n, which represent the dimensions of an m x n grid. A robot is initially located in the top-left corner of this grid (cell (0, 0)). The robot can only move either down or right at any point in time. The goal is for the robot to reach the bottom-right corner of the grid (cell (m - 1, n - 1)). Your task is to determine how many unique paths exist for the robot to get from the top-left corner to the bottom-right corner.
Example 1
- Input:
m = 3, n = 7 - Output:
28 - Explanation:
There are 28 distinct ways for the robot to reach the bottom-right corner by only moving right or down.
Example 2
- Input:
m = 3, n = 2 - Output:
3 - Explanation:
The three unique paths are:- Right → Down → Down
- Down → Down → Right
- Down → Right → Down
Example 3
- Input:
m = 7, n = 3 - Output:
28 - Explanation:
Similarly, there are 28 distinct ways for the robot to move from (0,0) to (6,2) using only right and down moves.
Hints and intuition
-
Hint 1:
Think about breaking the question into subproblems. The number of ways to reach any given cell is the sum of the ways to reach the cell above it and the ways to reach the cell on its left. -
Hint 2:
Consider using dynamic programming (DP) to build up a solution for the entire grid. Alternatively, notice that the robot must make exactly (m-1) down moves and (n-1) right moves in some order, which leads to a combinatorial solution.
Hint 1 is the whole pattern in one line. Unique Paths is a multidimensional dynamic programming question, built on a grid table rather than a take-or-skip choice. Every member of that family has the same shape. You fill a table, and each entry combines a fixed set of earlier entries, one per allowed choice. Knapsack offers two choices, take the item or skip it. Here the two choices that reach a cell are arrive from above and arrive from the left. Recognizing that shape is worth more than memorizing this one answer, which is why most short lists of recurring coding interview patterns put table-building DP near the top.
Detailed approaches
Approach A: dynamic programming
Idea
- Create a 2D DP array
dpwheredp[i][j]represents the number of unique paths to cell (i, j). - Base cases:
- The starting cell
dp[0][0]is 1 (only one way to be at the start). - For the first row (i = 0), there is only one way to move (all rights) so
dp[0][j] = 1. - For the first column (j = 0), there is only one way to move (all downs) so
dp[i][0] = 1.
- The starting cell
- Recurrence:
For any cell (i, j), the number of paths to reach it is:
This holds because the robot can only arrive from the top or from the left.dp[i][j] = dp[i-1][j] + dp[i][j-1]
Walkthrough
- Initialization:
Create a griddpof size m x n. Initialize the first row and first column to 1. - Fill the DP table:
For each cell (i, j) starting from (1, 1), computedp[i][j]using the recurrence. - Final answer:
The value atdp[m-1][n-1]is the number of unique paths from the start to the finish.
Every cell is computed once and read at most twice, so the table never revisits work. That is the payoff of the DP formulation, and it is the same machinery that drives harder grid questions such as Edit Distance, where each cell combines three neighbors instead of two.
Approach B: combinatorial mathematics
Idea
- To reach the bottom-right corner, the robot must make exactly (m-1) downs and (n-1) rights in any order.
- The total number of moves required is (m-1 + n-1) = (m+n-2).
- Counting paths therefore means counting the distinct arrangements of those moves, which the binomial coefficient gives directly:
Number of unique paths = C(m+n-2, m-1) = (m+n-2)! / [(m-1)! * (n-1)!]
Walkthrough
- Total moves:
Calculate the total number of moves (m+n-2). - Choose moves:
Choose which moves are downs, or equivalently which are rights. The answer is the number of ways to choose (m-1) moves from (m+n-2). - Final answer:
Compute the binomial coefficient to get the answer.
Approach B is faster and shorter, but it only works because movement is unrestricted. Add a single blocked cell, as Unique Paths II does, and the formula collapses while the DP table survives with a one-line change. Learn both, and lead with the DP in an interview unless the interviewer asks for the closed form.
Code implementation
Python implementation (dynamic programming)
Java implementation (dynamic programming)
Complexity analysis
Dynamic programming approach
-
Time complexity:
The solution uses two nested loops to fill the dp table, resulting in O(m * n) time. -
Space complexity:
The dp table requires O(m * n) space. (This can be optimized to O(n) by using a single array if needed.)
Combinatorial approach
- Time complexity:
Calculating the binomial coefficient can be done in O(m+n) time if optimized properly. - Space complexity:
Only a few variables are needed, so it is O(1).
Common pitfalls and edge cases
Common mistakes
-
Boundary conditions:
Leaving the first row and first column uninitialized produces wrong counts everywhere downstream. -
Off-by-one errors:
Indexing the dp table incorrectly, usually by looping to m or n instead of m-1 or n-1. -
Integer overflow:
In the combinatorial approach, computing the factorials outright overflows for larger inputs. Interleaving the multiplications and divisions keeps the running value small and avoids the overflow.
Edge cases
- Minimum grid size:
When m = 1 or n = 1, there is only one unique path. - Large values:
Confirm your approach stays fast as m and n grow. The DP approach at O(m*n) time handles moderate grid sizes comfortably.
Related problems for further practice
- Unique Paths II:
The same grid, now with obstacles in some cells. - Minimum Path Sum:
Find the path with the minimum sum of values from the top-left to the bottom-right of a grid. - Climbing Stairs:
A one-dimensional version of the same counting recurrence, where you count the ways to reach the top of a staircase. - Grid unique paths variations:
Grids that permit more directions, or that add constraints such as a move budget.
These questions repay practice as a group, not one at a time. Most of them appear on the Blind 75 list, and Grokking the Coding Interview teaches this one in its Unique Paths lesson alongside the other grid and table problems.

GET YOUR FREE
Coding Questions Catalog

$98

$197

$72