Typorithm · medium

Unique Paths

▷ Interactive algorithm labunique-paths-dpdynamic-programminggridcountingspace-optimized
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Unique Paths

0%

Dynamic Programming

Unique Paths counts grid routes using the fact that each cell is reached from top or left.

The robot only moves right or down.

Each cell adds paths from top and left.

A single row array stores the previous row plus current left value.

Preparing algorithm lab…
01

State: paths to current column.

02

Base: first row and column have one path.

03

Transition: top + left.

04

Space O(n).

function uniquePaths(m, n) {
  const dp = Array(n).fill(1);

  for (let row = 1; row < m; row++) {
    for (let col = 1; col < n; col++) {
      dp[col] += dp[col - 1];
    }
  }

  return dp[n - 1];
}

Read the implementation

Connect each part of the code to the concept before you type.

1. First row

Every cell in the first row has one path.

const dp = Array(n).fill(1)

2. Process rows

Start after the first row base case.

for (let row = 1; row < m; row++)

3. Top plus left

Current paths are from top plus left.

dp[col] += dp[col - 1]

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Unique Paths: Visual Walkthrough & Practice · Typorithm | ApplyDjinn