SprintCode.pro

Подготовка к алгоритмическим задачам

Super

Unique Paths

Description: A robot starts at the top-left of an m × n grid and can only move right or down. How many distinct paths lead to the bottom-right corner?

Example 1:

Input: m = 3, n = 7
Output: 28

Example 2:

Input: m = 3, n = 2
Output: 3

Constraints:

1 <= m, n <= 100

Recommended time and space complexity

Aim for O(m × n) time and O(n) space.


Hint 1

Any cell is reachable only from above or from the left.


Hint 2

So paths to a cell equal paths from above plus paths from the left.


Hint 3

Computing the current row needs only the previous one.

A two-dimensional dynamic programming problem. Teaches counting grid paths and reducing memory to a single row.

Expected Input :

3, 7

Expected Output

28