Problem
There is a robot on an m x n
grid. The robot is initially located at the top-left corner (i.e., grid[0][0]
). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]
). The robot can only move either down or right at any point in time.
Given the two integers m
and n
, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
OR
There is a rat in a maze of size m x n
, moving from top left corner to bottom right corner, moving only in two directions - down or right.
The test cases are generated so that the answer will be less than or equal to 2 * 10^9
.
Note: m and n will be such that the resulting answer fits in a 32 bit signed integer.
Examples
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Example 4:
|
|
Solution
Method 1 - DFS and Recursion
Since we know that one can only move either down or right at any point in time
- Base case:
f(N, 0) = 1, f(0, N) = 1
- Recursion:
f(N, N) = f(N, N-1) + f(N-1, N)
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(2^max(m,n))
- 🧺 Space complexity:
O(max(m, n))
Method 2 - Top Down DP with Memoization
We have a lot of repetition in Method 1, so we can use cache.
Code
|
|
Complexity
- ⏰ Time complexity:
O(m*n)
- 🧺 Space complexity:
O(m*n)
Method 3 - Bottom up DP
We can start filling numbers of ways we can reach to point [i,j]
from [0,0]
. For the first row, we can just reach in one way, from left to right. Likewise, for first column, we can reach in 1 way, i.e. top to bottom. For the 4X5
matrix:
Now, that we have the base case, we can start filling further. Now we will start filling row wise. For eg. For first row, we can either come from left or top, hence 2 ways. Likewise, we can fill remaining row. Total:
|
|
Code
|
|
Method 4 - Mathematics
This problem can be modelled as a math combinatorics problem.
- We start at
(0, 0)
cell and end at(m-1, n-1)
cell. - We have to make
m-1
down-moves andn-1
right-moves to reach the destination cell. - Hence, we need to perform a total number of
m+n-2
moves. - At each cell along the path, we can choose either the right-move or down-move and we need to find the number of unique combinations of these choices (which eventually leads to unique paths).
- This is nothing but calculating the number of different ways to choose
m-1
down-moves andn-1
right-moves from a total ofm+n-2
moves.
Hence we have to calculate:
$$ \text{Number of moves} = \binom{m + n - 2}{m - 1} OR \binom{m + n - 2}{n - 1} $$
we can use Binomial Coefficient to do so. Or we can solve the relation below:
$$ \binom{m + n - 2}{m - 1} = \binom{m + n - 2}{n - 1} = \frac{(m + n - 2)!}{(m - 1)!(n-1)!} $$ $$ \implies \binom{m + n - 2}{m - 1} = \frac{(m + n - 2) * (m + n - 3) * … * (m + n - n) * (m - 1)!}{(m - 1)!(n-1)!} $$ $$ \implies = \frac{(m + n - 2) * (m + n - 3) * … * m}{(n-1)!} $$ $$ \implies = \frac{(m + n - 2) * (m + n - 3) * … * m}{(n-1) * (n - 2) … * 2 * 1} $$
We could have cancelled out the (n-1)!
as well in the above evaluation, but we will do one of those based on min(m,n)
to give best time complexity in the solution below.
Code
|
|
Complexity
- ⏰ Time complexity:
O(max(m, n))
- 🧺 Space complexity:
O(1)