Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Dynamic Programming High frequency Solution Available Practice Mode

Unique Paths LeetCode Solution

Tags: #Math #DP #Combinatorics

Approach Summary

dp[i][j] = paths from (0,0) to (i,j) = dp[i-1][j] + dp[i][j-1]. Row/column of 1s are base cases. Space-optimized to O(n).

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Dynamic Programming Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Dynamic Programming pattern.

Framework: Dynamic Programming • Interview Prep: Tested at FAANG • Explore all Dynamic Programming problems & invariants →

How to Recognize This Pattern

  • Grid path counting
  • Only move right or down

Complexity Analysis

Time Complexity

O(m × n)

Space Complexity

O(n)

Tags

Math DP Combinatorics

Frequently Asked Questions

What is the optimal time and space complexity for Unique Paths?
The optimal solution for Unique Paths achieves O(m × n) runtime complexity and requires O(n) auxiliary space. Time complexity is O(m * n) since every grid cell is visited once. Space complexity is O(n) using a rolling 1D row array.
How do I identify that Unique Paths requires the Dynamic Programming pattern?
In technical interviews, look for key problem signals: Grid path counting; Only move right or down. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Dynamic Programming strategy.
What critical edge cases must be handled in Unique Paths?
Essential edge cases to verify include: m = 1 or n = 1 (only a single row or column; robot has only 1 path straight to end, returns 1); m = 3, n = 7 (standard LeetCode test case; returns 28); Square grids (e.g. m = 3, n = 3; returns 6). Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Unique Paths?
Unique Paths is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Google, Apple.
Buy me a coffee
Daily Practice
0/0