Unique Paths (Space Optimized)

Given m and n, return the number of monotonic paths from the top-left to the bottom-right of an m x n grid using only right and down moves. Pattern focus: Space optimization. Solve the classic Unique Paths problem with a rolling array or 1D DP.

Input Format

m and n are the grid dimensions

Output Format

number of monotonic paths

Constraints

  • 1 <= m, n <= 1000

Examples

Example 1:

Input:

m = 4
n = 5

Output:

35

Explanation:

A 4x5 grid has 35 monotonic paths.

Example 2:

Input:

m = 5
n = 5

Output:

70

Explanation:

A 5x5 grid has 70 monotonic paths.

Example 3:

Input:

m = 2
n = 6

Output:

6

Explanation:

A 2x6 grid has 6 paths.

Loading...
Unique Paths (Space Optimized) - Dp Grid