Back to Practice
#0067

Unique Paths in a Grid

MediumDSA25 min20 XP

Problem

Given m rows and n columns, return the number of ways to move from the top-left cell to the bottom-right cell using only right and down moves.

Why This Matters

2D DP becomes less scary when you see that each cell only depends on the cell above and the cell to the left.

Function Signature

def unique_paths(m, n):

Examples

Example 1
Inputm = 3, n = 7
Output28

Every interior cell combines paths from above and left.

Constraints

  • m and n are positive integers.
  • No obstacles are present.
  • Return an integer count.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Three by seven grid
Input3, 7
Expected28

Classic small grid example.

Single row
Input1, 5
Expected1

Only keep moving right.

Hidden Test Categories
single columnsquare gridsmall 2x2 grid