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.
The test cases are generated so that the answer will be less than or equal to 2 * 109.
Example 1:
Input: m = 3, n = 7 Output: 28
Example 2:
Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down
Constraints:
1 <= m, n <= 100When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are standing at the top-left corner of a grid and need to walk to the bottom-right corner, only ever moving right or down. The brute force approach involves physically walking every single possible path from start to finish to see how many valid routes exist.
Here's how the algorithm would work step-by-step:
def unique_paths(grid_rows, grid_columns):
def explore_paths(current_row, current_column):
# Base case to count a successful complete journey when the destination is reached
if current_row == grid_rows - 1 and current_column == grid_columns - 1:
return 1
# Stop exploring further if we move outside the grid boundaries
if current_row >= grid_rows or current_column >= grid_columns:
return 0
# Branch into both valid directions to explore every possible route to the end
paths_going_right = explore_paths(current_row, current_column + 1)
paths_going_down = explore_paths(current_row + 1, current_column)
return paths_going_right + paths_going_down
return explore_paths(0, 0)Instead of exploring every possible path from start to finish, the optimal approach works backward from the destination by adding up the known paths from previous steps. This prevents repeating the same work and instantly solves the puzzle by recognizing that the number of ways to reach any spot is simply the sum of the ways to reach the spot directly above it and the spot directly to its left.
Here's how the algorithm would work step-by-step:
class Solution:
def uniquePaths(self, number_of_rows: int, number_of_cols: int) -> int:
# Initialize a grid with ones to represent the base paths at the edges.
path_counts = [[1] * number_of_cols for _ in range(number_of_rows)]
# Iterate backward from the bottom-right corner to accumulate path counts.
for row_index in range(number_of_rows - 2, -1, -1):
for col_index in range(number_of_cols - 2, -1, -1):
# Sum paths from the right and below to find total ways from current spot.
path_counts[row_index][col_index] = path_counts[row_index + 1][col_index] + path_counts[row_index][col_index + 1]
return path_counts[0][0]| Case | How to Handle |
|---|---|
| Grid with only one row or one column (m=1 or n=1) | The DP algorithm naturally initializes and computes a single path since only one direction of movement is possible. |
| Minimum possible grid size of 1x1 | The solution immediately returns 1 since the robot is already at the destination. |
| Large grid dimensions causing integer overflow | Using 64-bit integers (long in Java or standard int in Python which handles arbitrarily large integers) prevents arithmetic overflow for large results. |
| Deep recursion causing stack overflow in a naive recursive approach | Using bottom-up dynamic programming or memoization limits the call stack depth and prevents stack overflow errors. |
| Extremely large grid dimensions causing memory limit exceeded | Optimizing space complexity from a 2D matrix to a 1D array reduces memory usage to O(n) space. |
| Asymmetric grids where rows are much greater than columns or vice versa | Iterating through the full dimensions correctly computes all state transitions regardless of grid aspect ratio. |