Dynamic Programming
Medium

Unique Paths

A robot is located at the top-left corner of a m x n grid. The robot can only move either down or right at any point in time. Find the number of unique paths to the bottom-right corner.
Problem Understanding

We need to count how many ways to reach (m-1, n-1) from (0, 0) moving only Right or Down.

Example: m = 3, n = 2

  • Right -> Down -> Down
  • Down -> Down -> Right
  • Down -> Right -> Down
  • Output: 3
Algorithm Strategy

This is a classic DP problem on a grid.

Key Insight:
To reach cell (i, j), you must have come from either:

  1. Top: (i-1, j)
  2. Left: (i, j-1)

Recurrence: dp[i][j] = dp[i-1][j] + dp[i][j-1]
Base Cases: dp[i][0] = 1 and dp[0][j] = 1 (only 1 way to reach cells in first row/col - go straight).

Interactive Visualization
Step 1 / 1

Initializing...

1x
See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep