#62

Unique Paths

Medium 2D grid DP

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A robot at the top-left of an m × n grid moves only right or down. Count the paths to the bottom-right.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: paths from the top-left to the bottom-right moving right or down.

Why this works: paths to a cell = paths from above + paths from the left.

Example: 3 × 3 grid

row 0: 1 1 1row 1: 1 2 3row 2: 1 3 6 → 6

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using int for the combinatorial formula C(m+n−2, m−1) without care — intermediate products overflow.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(n).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon