TRT
Sign in
mediumAdvancedLeetCode#grid DP

Unique paths

Count the paths from the top-left to the bottom-right of a grid moving only right or down, with a grid DP.

The Runtime Theory Team1 min read
Taught inGrids and stringsPro — members onlyTwo-dimensional DP fills a table whose cells depend on their neighbors above and to the left. On grids it counts or optimizes paths; on two strings, a cell compares two prefixes, which is how diff tools, spell checkers and DNA alignment work.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inm = 3, n = 7

out28

inm = 3, n = 2

out3

inm = 1, n = 1

out1

A robot moves only right or down on an m × n grid. How many paths lead from the top-left to the bottom-right cell?

Before you code

Each cell is entered from above or from the left, so its count is the sum of those two counts.

Hints

  1. Row 0 and column 0 are all 1.
  2. dp[r][c] = dp[r-1][c] + dp[r][c-1].
  3. One row is enough. The closed form is C(m + n − 2, m − 1).

Production & complexity notes

When solving Unique paths on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Grids and strings to reinforce the underlying pattern.

What it practices

Row-by-row grid tables, from Grids and strings.

Not started

Sign in to save your learning progress.

Sign in to save