Skip to content

Practice Unique Paths

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#62
LeetCode Problem

Unique Paths

Problem Statement

Count paths from the upper-left cell to the lower-right cell of an m-row, n-column grid. Each move advances exactly one cell right or one cell down. The answer for supplied inputs is at most 2,000,000,000. Approach: The number of paths reaching a cell is the sum of the paths reaching the cell above and the cell to its left. Initialize the first row and first column to one, then fill the remaining cells. A rolling row uses O(n) space and O(mn) time. A one-row or one-column grid has exactly one path.

Source: Unique Paths on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
m = 2, n = 4
Output
4
Explanation

A route needs one down move and three right moves; the down move can occupy any of four positions.

# Example 2

Input
m = 1, n = 5
Output
1
Explanation

Every move must go right.

Constraints

1 <= m, n <= 100

Related LeetCode Problems

Practice more problems similar to Unique Paths.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.