Introduction
The Unique Paths problem is one of the most important Grid Dynamic Programming interview questions.
You are given:
m rows
n columns A robot starts at:
Top Left Cell Goal:
Reach Bottom Right Cell Allowed Moves:
Move Right or Move DownFind:
Total number of unique paths. Example
m = 3
n = 7
Output = 28 Key Observation
To reach any cell:
(i,j) Robot can come from:
Top Cell (i-1,j) or Left Cell (i,j-1) Therefore:
dp[i][j] = dp[i-1][j] + dp[i][j-1]This is the DP recurrence.
Approach 1 : Memoization
Explanation
Use recursion and store results.
Steps:
- Solve smaller states.
- Save answers.
- Reuse computations.
- Avoid repeated work.
Approach 2 : Tabulation
Explanation
Build DP table from top-left to bottom-right.
Base Case:
First Row = 1First Column = 1
Because:
Only one way to reach them. Transition:
dp[i][j] = dp[i-1][j] + dp[i][j-1] Dry Run
Input:
m = 3n = 3DP Table:
1 1 1
1 2 31 3 6
Answer:
6 Approach 3 : Space Optimized DP
Explanation
Instead of storing:
m × n table Store only:
Current Row This reduces space complexity.