DSADPGrid

Unique Paths Visualizer

What is Unique Paths

A robot may only move right or down. The number of paths to cell (i, j) is paths from above plus paths from the left. The first row and first column are all 1 — only one hallway into each of those cells.

  • dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • Combinatorics: choose m−1 downs among (m−1)+(n−1) moves = C(m+n−2, m−1).
  • One rolling row is enough extra memory.

Brute force approach

From (r, c) walk down or right. The number of paths is binomial, but the recursion tree is still exponential.

From each cell the robot goes down or right. Cells are reached by many routes. Memoization stores the path count of each cell.

Try a small input

Keep the grid at most 3 by 3. A 3 by 2 grid has 3 paths.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def paths(r, c):
if at the end: return 1
return paths(r+1, c) + paths(r, c+1)
Step 0

Memoized approach

dp[j] += dp[j-1] on a rolling row. First row and column stay 1. Time O(mn).

Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.

Memoized recursion
def paths(r, c, memo):
if (r, c) in memo: return memo
if at the end: return 1
memo = down + right
return memo
Step 0

How this visualizer works

Grid 3×4. Watch every cell become the sum of its top and left neighbours. Bottom-right is 10. Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.

Interactive DP trace

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def uniquePaths(m, n):
2dp = [1]*n
3for _ in range(1, m):
4for j in range(1, n):
5dp[j] += dp[j-1]
6return dp[-1]

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def uniquePaths(m, n):
2dp = [1]*n
3for _ in range(1, m):
4for j in range(1, n):
5dp[j] += dp[j-1]
6return dp[-1]

complexity.md

OperationTime
Each cellO(1)
Fill gridO(mn)
SpaceO(n)

Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.

Implementations

dp.py

1def paths_brute(m, n, r=0, c=0):
2if r == m - 1 and c == n - 1:
3return 1
4if r >= m or c >= n:
5return 0
6down = paths_brute(m, n, r + 1, c)
7right = paths_brute(m, n, r, c + 1)
8return down + right
1def paths_dp(m, n):
2dp = [1] * n
3for _ in range(1, m):
4for j in range(1, n):
5dp[j] += dp[j - 1]
6return dp[-1]
1int paths_brute(int m, int n, int r, int c) {
2if (r == m - 1 && c == n - 1) {
3return 1;
4}
5if (r >= m || c >= n) {
6return 0;
7}
8return paths_brute(m, n, r + 1, c) + paths_brute(m, n, r, c + 1);
9}
1int paths_dp(int m, int n) {
2int dp[101];
3int i;
4int j;
5for (j = 0; j < n; j++) {
6dp[j] = 1;
7}
8for (i = 1; i < m; i++) {
9for (j = 1; j < n; j++) {
10dp[j] += dp[j - 1];
11}
12}
13return dp[n - 1];
14}