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).
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.
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.
1 <= m, n <= 1002000000000.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).
There are eight moves in total: two downs and six rights. Choose the two down positions among eight positions: . Each choice determines one distinct route.
One down and one right can be ordered as DR or RD. Reaching the same destination does not make their move sequences identical.
The robot already occupies the destination. The empty move sequence is one valid route, so the answer is 1, not 0.
There is no room for a down move. The only route consists of 99 right moves; maximum width alone does not imply many routes.
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.
Reveal Brute Force Recursion: intuition, complexity, and code
3 x 2, write every ordering of two D moves and one R move.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.1 x 1 grid has one empty route. Both approaches must return 1 without requiring a move.m and n does not change the answer: exchanging down and right creates a one-to-one correspondence between routes.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.100 x 100 is outside the guaranteed input set because its route count exceeds 2000000000. Do not use it as a valid overflow test.m + n moves or choosing m downs counts steps from dimensions instead of subtracting the already occupied starting row and column.0 at the destination loses every completed route. The destination contributes one empty suffix.(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.18 x 17. Starting res as long makes multiplication wide before division.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.