मुख्य कंटेंट तक स्किप करें

Unique Paths

KANISHKA GUPTA
EditReport

Description:

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 * 10^9.

Video Explanation

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

Approaches:

1. Dynamic Programming (Tabulation)

The problem asks for the total number of unique paths to a specific cell. The number of ways to reach cell (i, j) is the sum of the ways to reach the cell directly above it (i-1, j) and the cell directly to its left (i, j-1).

We can create a 2D table dp of size m x n. We initialize the first row and the first column to 1 because there is only one way to reach any cell in the first row (by moving strictly right) or the first column (by moving strictly down). For all other cells, dp[i][j] = dp[i-1][j] + dp[i][j-1].

  • Time Complexity: O(m×n)O(m \times n) because we iterate through every cell in the grid once.
  • Space Complexity: O(m×n)O(m \times n) to store the DP table. (Note: This can be further space-optimized to O(n)O(n) by keeping track of only the previous row).

DP Solutions:

Solutions

class Solution {
public:
int uniquePaths(int m, int n) {
std::vector<std::vector<int>> dp(m, std::vector<int>(n, 1));

for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}

return dp[m-1][n-1];
}
};
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

💬 Discuss this page

Have a question or spot something confusing in "Unique Paths"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.