LeetCode Problem

How to Solve Unique Paths

In the Unique Paths problem, a robot moves from the top-left to the bottom-right corner of a grid by moving only right or down. The challenge is to compute the number of unique paths given the grid dimensions, utilizing dynamic programming techniques and combinatorial reasoning to optimize the solution.

GhostInterview Help

Need help with Unique Paths without spending extra time grinding it?

GhostInterview can read Unique Paths from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #62State transition dynamic programmingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

In the Unique Paths problem, a robot moves from the top-left to the bottom-right corner of a grid by moving only right or down. The challenge is to compute the number of unique paths given the grid dimensions, utilizing dynamic programming techniques and combinatorial reasoning to optimize the solution.

Problem Statement

A robot is positioned at the top-left corner of an m x n grid and must move to the bottom-right corner. The robot can only move either right or down at each step. The task is to calculate the number of unique paths the robot can take to reach the destination.

Given the grid dimensions m and n, return the number of possible unique paths. The problem guarantees that the answer will not exceed 2 * 10^9.

Examples

Example 1

Input: m = 3, n = 7

Output: 28

Example details omitted.

Example 2

Input: m = 3, n = 2

Output: 3

From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:

  1. Right -> Down -> Down
  2. Down -> Down -> Right
  3. Down -> Right -> Down

Constraints

  • 1 <= m, n <= 100

Solution Approach

Combinatorics Approach

One way to approach this problem is by using combinatorics. The robot needs to make exactly m-1 down movements and n-1 right movements, which results in a total of m+n-2 steps. The number of unique paths is equivalent to choosing (m-1) down movements from (m+n-2) total steps, or vice versa, which can be computed using the binomial coefficient formula.

Dynamic Programming Approach

A more efficient approach is using dynamic programming. You can create a 2D table where each cell represents the number of ways to reach that cell from the starting point. You initialize the first row and column as 1, since there is only one way to reach any cell in the first row (by moving right) or the first column (by moving down). Then, for each cell, you sum the values from the cell above and to the left.

Optimized Space Complexity Approach

The dynamic programming approach can be optimized in terms of space complexity. Instead of maintaining a 2D table, we can use a 1D array to store the current row's results. By iterating over the array from right to left, updating the value for the current cell based on the cell to its left, we can reduce space usage while preserving the results.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the combinatorial approach is O(m+n), which is the time required to compute the binomial coefficient. The dynamic programming solution has a time complexity of O(mn), where m and n are the grid dimensions. The space complexity of the 2D dynamic programming solution is O(mn), but this can be reduced to O(n) with the optimized approach that uses a 1D array.

What Interviewers Usually Probe

  • Tests whether the candidate understands combinatorics and dynamic programming.
  • Evaluates the ability to optimize space complexity in dynamic programming solutions.
  • Assesses familiarity with efficient solution strategies for grid-based problems.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to initialize the first row and column of the dynamic programming table.
  • Misunderstanding the combinatorics formula for calculating the number of paths.
  • Using an inefficient solution with excessive space complexity when an optimized approach is possible.

Follow-up variants

  • Modified problem where the robot can move in more directions (e.g., up, left, right, down).
  • Unique paths problem with obstacles in the grid, requiring pathfinding around blocked cells.
  • Grid size variation with constraints on maximum m or n values for more challenging scenarios.

How GhostInterview Helps

  • GhostInterview helps by offering step-by-step hints for dynamic programming solutions, guiding you through the construction of the state transition table.
  • It provides detailed analysis on time and space complexity trade-offs, assisting you in selecting the most efficient approach based on grid dimensions.
  • GhostInterview emphasizes combinatorics and its application in dynamic programming, making sure you understand both methods and can apply them effectively in interviews.

Topic Pages

FAQ

What is the most efficient way to solve the Unique Paths problem?

The most efficient way is using dynamic programming with a 1D array to optimize space complexity while preserving time efficiency. This reduces space usage to O(n).

How can combinatorics be used to solve the Unique Paths problem?

Combinatorics can be used by calculating the number of ways to arrange the required down and right movements in a sequence of total steps using the binomial coefficient formula.

What is the time complexity of the dynamic programming solution?

The time complexity of the dynamic programming solution is O(m*n), where m and n are the grid dimensions.

Can the grid dimensions be larger than 100?

No, the problem constraints ensure that the grid dimensions m and n will always be between 1 and 100.

What is the binomial coefficient formula used for in this problem?

The binomial coefficient formula is used to calculate the number of ways to choose the down or right movements from the total steps, which determines the number of unique paths.

GhostInterview Solver

Need direct help with Unique Paths instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Unique Paths from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.