iq.lab
Python starts when a code cell comes near or you run one
mediumDynamic programming, 2D target 25 min

Unique paths

A cart stands in the top-left cell of a warehouse floor drawn as a grid with rows rows and cols columns. It must reach the bottom-right cell. Each move takes it one cell right or one cell down, never left or up.

Return the number of different routes from the top-left cell to the bottom-right cell. Two routes are different when their lists of moves are different. On a grid of one cell the cart is already there, and staying put counts as one route.

Example 1
Inputrows = 3, cols = 3Output6

Every route makes 2 moves right (R) and 2 moves down (D) in some order: RRDD, RDRD, RDDR, DRRD, DRDR, DDRR.

Example 2
Inputrows = 2, cols = 4Output4

Three R and one D. The D can go in any of 4 places: DRRR, RDRR, RRDR, RRRD.

Example 3
Inputrows = 1, cols = 5Output1

A single row: the only route is four moves right.

Constraints
  • 1 ≤ rows, cols ≤ 100

  • Python integers do not overflow, so even very large counts come out exact.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.