Unique Paths
LeetCode 62 · Medium · solution, intuition & step-by-step walkthrough
paths(i, j) = paths(i-1, j) + paths(i, j-1) — or simply C(m+n-2, n-1).
This is how I solved Unique Paths (LeetCode 62), a medium problem from the Grid DP pattern. Below: the intuition, the approach step by step, my accepted Python code and the 3 versions I wrote on the way, animated test cases and a line-by-line debugger.
Intuition
Every cell can be entered from the left or from above, so the number of routes into it is the routes into the left cell plus the routes into the cell above. Cells in the first row or column have exactly one route. There is also a counting view: every path is m-1 downs and n-1 rights in some order.
Approach
- 1
Top-down: dfs(i, j) with first row/column returning 1, cached with lru_cache.
- 2
Bottom-up: set the first row and column to 1, then fill the rest with the sum rule.
- 3
Combinatorics: choose which n-1 of the m+n-2 moves are 'right' — compute nCr incrementally to avoid huge factorials.
How I solved it · from first idea to final
Sum of the two incoming directions, cached.
Fill the grid from the first row and column.
Count arrangements of downs and rights.
Recursion + memoPythonO(m·n) time · O(m·n) space
from functools import lru_cache
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
@lru_cache(maxsize=None)
def dfs(i,j):
if i==0 or j==0:
return 1
if i<0 or j<0:
return 0
return dfs(i-1,j)+dfs(i,j-1)
return dfs(m-1,n-1)TabulationPythonO(m·n) time · O(m·n) space
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp=[[0]*n for _ in range(m)]
for i in range(m-1,-1,-1):
dp[i][0]=1
for j in range(n-1,-1,-1):
dp[0][j]=1
for i in range(1,m):
for j in range(1,n):
dp[i][j]=dp[i-1][j]+dp[i][j-1]
return dp[m-1][n-1]Combinatorics nCrPythonO(n) time · O(1) space
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
def factorial(n,r):
ans = 1
for i in range(r):
ans=ans*(n-i)
ans=ans//(i+1)
return ans
return factorial(m+n-2,n-1)Unique Paths — FAQ
What is the intuition behind Unique Paths?
Every cell can be entered from the left or from above, so the number of routes into it is the routes into the left cell plus the routes into the cell above. Cells in the first row or column have exactly one route. There is also a counting view: every path is m-1 downs and n-1 rights in some order.
How do you solve Unique Paths (LeetCode 62)?
1. Top-down: dfs(i, j) with first row/column returning 1, cached with lru_cache. 2. Bottom-up: set the first row and column to 1, then fill the rest with the sum rule. 3. Combinatorics: choose which n-1 of the m+n-2 moves are 'right' — compute nCr incrementally to avoid huge factorials.
What is the time and space complexity of Unique Paths?
Time: O(min(m, n)) for the formula · O(m·n) for the DP. Space: O(1) for the formula. (Accepted approach: Combinatorics nCr.)
What are common mistakes in Unique Paths?
In the memo version the i == 0 or j == 0 check must come before the negative check (it does). Multiply then integer-divide at each step so the running value stays an exact integer.
Is there more than one way to solve Unique Paths?
Recursion + memo (O(m·n) time · O(m·n) space): Sum of the two incoming directions, cached. Tabulation (O(m·n) time · O(m·n) space): Fill the grid from the first row and column. Combinatorics nCr (O(n) time · O(1) space): Count arrangements of downs and rights.