← Back to DSA Animator
Unique Paths LC #62 Medium 2D DP
Problem

A robot on an m × n grid starts at top-left and moves to bottom-right. It can only move right or down. Return the number of unique paths.

Example 1
Input: m = 3, n = 7
Output: 28
Example 2
Input: m = 3, n = 3
Output: 6
Constraints: 1 ≤ m, n ≤ 100
Try Examples
Custom:
Approach
2D DP — dp[r][c] = paths to reach (r,c)
Recurrence: dp[r][c] = top + left. Base: dp[0][0]=1. Robot moves right or down only.
Grid Fill
Load an example to begin.
Variables
cell (r,c)
top
left
paths
Step Logic
Press ▶ Play or Next Step to begin.
🎯
Ready
0 / 0
Select an example and press Play.
Algorithm
1
dp[r][c] = paths to reach (r,c). Only right or down.
2
dp[r][c] = top + left (paths from above + from left)
3
Base: dp[0][0]=1. First row & col = 1.
4
Answer = dp[m-1][n-1]
Time
O(m×n)
Space
O(n)
Why This Works

To reach (r,c) you must come from (r-1,c) or (r,c-1). So paths[r][c] = paths from above + paths from left. First row and column are 1 (one straight path). Fill top-to-bottom, left-to-right so dependencies are ready.