KatInterview

62. Unique Paths

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the number of unique paths a robot can take to travel from the top-left corner to the bottom-right corner of an m x n grid. The robot can only move down or right. This is a classic path-counting problem on a grid, which can be solved efficiently using dynamic programming or combinatorics.

Solution

Unique Paths in Python

class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        # We use a 1D DP array of size n for space optimization.
        # dp[j] will store the number of paths to reach cell (i, j).
        
        # Initialize dp array with 1s. This represents the base case for the first row,
        # as there's only one way to reach any cell in the first row (by moving right).
        dp = [1] * n
        
        # Iterate from the second row (i=1) to the last row (m-1).
        for i in range(1, m):
            # Iterate from the second column (j=1) to the last column (n-1).
            for j in range(1, n):
                # The number of paths to cell (i, j) is the sum of paths to the cell
                # above (i-1, j) and the cell to the left (i, j-1).
                # In our 1D DP array:
                # paths to (i-1, j) is the old value of dp[j].
                # paths to (i, j-1) is the new (already updated in this row's iteration) value of dp[j-1].
                dp[j] = dp[j] + dp[j-1]
        
        # The result is the number of paths to the bottom-right corner (m-1, n-1),
        # which is stored in the last element of our dp array.
        return dp[n - 1]

Unique Paths in Java

class Solution {
    public int uniquePaths(int m, int n) {
        // We use a 1D DP array of size n for space optimization.
        // dp[j] will store the number of paths to reach cell (i, j).
        int[] dp = new int[n];

        // Initialize dp array with 1s. This represents the base case for the first row,
        // as there's only one way to reach any cell in the first row (by moving right).
        for (int j = 0; j < n; j++) {
            dp[j] = 1;
        }

        // Iterate from the second row (i=1) to the last row (m-1).
        for (int i = 1; i < m; i++) {
            // Iterate from the second column (j=1) to the last column (n-1).
            for (int j = 1; j < n; j++) {
                // The number of paths to cell (i, j) is the sum of paths to the cell
                // above (i-1, j) and the cell to the left (i, j-1).
                // In our 1D DP array:
                // paths to (i-1, j) is the old value of dp[j].
                // paths to (i, j-1) is the new (already updated in this row's iteration) value of dp[j-1].
                dp[j] = dp[j] + dp[j-1];
            }
        }

        // The result is the number of paths to the bottom-right corner (m-1, n-1),
        // which is stored in the last element of our dp array.
        return dp[n - 1];
    }
}

Unique Paths in C++

class Solution {
public:
    int uniquePaths(int m, int n) {
        // We use a 1D DP vector of size n for space optimization.
        // dp[j] will store the number of paths to reach cell (i, j).
        // Initialize with 1s, as there is one way to reach any cell in the first row.
        std::vector<int> dp(n, 1);

        // Iterate from the second row (i=1) to the last row (m-1).
        for (int i = 1; i < m; ++i) {
            // Iterate from the second column (j=1) to the last column (n-1).
            for (int j = 1; j < n; ++j) {
                // The number of paths to cell (i, j) is the sum of paths to the cell
                // above (i-1, j) and the cell to the left (i, j-1).
                // In our 1D DP array:
                // paths to (i-1, j) is the old value of dp[j].
                // paths to (i, j-1) is the new (already updated in this row's iteration) value of dp[j-1].
                dp[j] = dp[j] + dp[j-1];
            }
        }

        // The result is the number of paths to the bottom-right corner (m-1, n-1),
        // which is stored in the last element of our dp vector.
        return dp[n - 1];
    }
};

Unique Paths in TypeScript

function uniquePaths(m: number, n: number): number {
    // We use a 1D DP array of size n for space optimization.
    // dp[j] will store the number of paths to reach cell (i, j).
    // Initialize with 1s for the first row.
    const dp = new Array(n).fill(1);

    // Iterate from the second row (i=1) to the last row (m-1).
    for (let i = 1; i < m; i++) {
        // Iterate from the second column (j=1) to the last column (n-1).
        for (let j = 1; j < n; j++) {
            // The number of paths to cell (i, j) is the sum of paths to the cell
            // above (i-1, j) and the cell to the left (i, j-1).
            // In our 1D DP array:
            // paths to (i-1, j) is the old value of dp[j].
            // paths to (i, j-1) is the new (already updated in this row's iteration) value of dp[j-1].
            dp[j] = dp[j] + dp[j-1];
        }
    }

    // The result is the number of paths to the bottom-right corner (m-1, n-1),
    // which is stored in the last element of our dp array.
    return dp[n - 1];
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.