Skip to content
AI360Xpert
Beta
Difficulty: Medium2-D Dynamic Programming

Unique Paths

Problem in Plain English

A robot on an m by n grid can only move down or right. Count how many different routes lead from the top-left corner to the bottom-right corner.

Problem Statement

A robot starts at the top-left cell (0, 0) of an m x n grid and must reach the bottom-right cell (m - 1, n - 1). Each move goes one cell down or one cell right, staying inside the grid.

Return the number of distinct routes. Two routes differ if their move sequences differ. There are no obstacles, and the starting cell itself requires no move. Inputs guarantee that the answer is at most 2000000000.

Constraints

  • 1 <= m, n <= 100
  • Inputs guarantee that the number of routes is at most 2000000000.

Examples

Example 1

Input
m = 3, n = 2
Output
3

Every route has two down moves D and one right move R. The three possible orders are DDR, DRD, and RDD; all remain inside the grid and end at (2, 1).

Example 2

Input
m = 3, n = 7
Output
28

There are eight moves in total: two downs and six rights. Choose the two down positions among eight positions: 8∗7/2=288 * 7 / 2 = 28. Each choice determines one distinct route.

Example 3

Input
m = 2, n = 2
Output
2

One down and one right can be ordered as DR or RD. Reaching the same destination does not make their move sequences identical.

Example 4

Input
m = 1, n = 1
Output
1

The robot already occupies the destination. The empty move sequence is one valid route, so the answer is 1, not 0.

Example 5

Input
m = 1, n = 100
Output
1

There is no room for a down move. The only route consists of 99 right moves; maximum width alone does not imply many routes.

Intuition

Count routes by the choices that distinguish them. In a 3 x 2 grid, every route makes two down moves and one right move: DDR, DRD, or RDD. Recursive search counts these choices one branch at a time, but different prefixes repeatedly reach the same cell. Because every route contains exactly the same numbers of the two move types and there are no obstacles, choosing the positions of the right moves counts the routes directly. This replaces repeated grid exploration with one binomial coefficient, the number of ways to choose positions from a fixed set.

Approaches

Brute Force Recursion

Solution Details

Reveal Brute Force Recursion: intuition, complexity, and code

Hints

Hint 1
From a cell, separate routes by whether their first move is down or right. What count should the destination contribute?
Hint 2
Every complete route makes the same numbers of downs and rights. For 3 x 2, write every ordering of two D moves and one R move.
Hint 3
Choosing the positions of the downs determines the rights too. Use C(m+n−2,m−1)C(m+n-2, m-1) or its symmetric choice for right positions.
Hint 4
Compute the coefficient incrementally with res = res * (total - r + i) / i. Explain why this entire quotient is integral and why the temporary product needs a wider type than the return value.

Edge Cases

  • A 1 x 1 grid has one empty route. Both approaches must return 1 without requiring a move.
  • A single row or column has one straight route. Recursion follows one chain, and the combinatorial loop has zero iterations.
  • Swapping m and n does not change the answer: exchanging down and right creates a one-to-one correspondence between routes.
  • For m = 18, n = 17, the answer is 1166803110. The last product is 18668849760 before division by 16, so Java needs long even though the result fits int.
  • The dimension limits and the answer promise both apply: 100 x 100 is outside the guaranteed input set because its route count exceeds 2000000000. Do not use it as a valid overflow test.
  • There are no blocked cells. Adding obstacles breaks the move-position bijection because some arrangements would hit a blocked cell; use cell-based DP for that variant.

Common Mistakes and Interview Tips

  • Using m + n moves or choosing m downs counts steps from dimensions instead of subtracting the already occupied starting row and column.
  • Returning 0 at the destination loses every completed route. The destination contributes one empty suffix.
  • Merging recursive calls just because they reach the same cell without preserving their counts loses distinct prefixes. Memoization may reuse a suffix count, but each incoming branch still adds it.
  • Building factorials for the binomial formula can overflow even when the final ratio is small. Divide at every multiplicative step instead.
  • Dividing (total - r + i) by i before multiplying can truncate: for 3 x 3, the second factor is 4 / 2, but other steps such as 3 x 4 include 5 / 2. The full product is divisible, not necessarily an individual factor.
  • Keeping the Java temporary product in int overflows on valid grids such as 18 x 17. Starting res as long makes multiplication wide before division.
  • Interview tip: justify both directions of the route-to-position correspondence, then state the answer promise before claiming that JavaScript arithmetic or the final Java cast is safe. Raw recursion is a teaching baseline, not a practical solution for highly branching large inputs.

Key Takeaway

When every solution orders a fixed multiset of choices and every ordering is legal, count position selections with a binomial coefficient instead of enumerating paths. Check the bijection and intermediate arithmetic bounds first; obstacles or extra movement rules can invalidate the shortcut.