Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Unique Paths II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "63" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given an m x n integer array grid. There is a robot 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. An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle. Return the number of possible unique paths that the robot can take to reach the bottom-right corner. The testcases are generated so that the answer will be less than or equal to 2 * 109. Example 1: Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]] Output: 2 Explanation: There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right Example 2: Input: obstacleGrid = [[0,1],[0,0]] Output: 1 Constraints: m == obstacleGrid.length n == obstacleGrid[i].length 1 <= m, n <= 100 obstacleGrid[i][j] is 0 or 1.

Explanation

Here's an efficient solution to the unique paths problem with obstacles, along with explanations:

  • Dynamic Programming: Use dynamic programming to store the number of paths to reach each cell. The number of paths to a cell is the sum of the paths from the cell above and the cell to the left.
  • Base Cases: Initialize the top-left cell based on whether it's an obstacle or not. The first row and first column are handled carefully, propagating path counts until an obstacle is encountered.
  • Obstacle Handling: If a cell contains an obstacle, the number of paths to that cell is 0.

  • Runtime Complexity: O(mn), where m is the number of rows and n is the number of columns. Storage Complexity: O(mn).

Code

    def uniquePathsWithObstacles(obstacleGrid):
    """
    Calculates the number of unique paths in a grid with obstacles.

    Args:
        obstacleGrid: A list of lists of integers representing the grid.

    Returns:
        The number of unique paths.
    """
    m = len(obstacleGrid)
    n = len(obstacleGrid[0])

    # If the starting cell has an obstacle, there are no paths.
    if obstacleGrid[0][0] == 1:
        return 0

    # Create a DP table to store the number of paths to each cell.
    dp = [[0] * n for _ in range(m)]

    # Initialize the starting cell.
    dp[0][0] = 1

    # Initialize the first column.
    for i in range(1, m):
        if obstacleGrid[i][0] == 0:
            dp[i][0] = dp[i-1][0]
        else:
            dp[i][0] = 0 # Obstacle blocks the path

    # Initialize the first row.
    for j in range(1, n):
        if obstacleGrid[0][j] == 0:
            dp[0][j] = dp[0][j-1]
        else:
            dp[0][j] = 0 # Obstacle blocks the path

    # Fill the DP table.
    for i in range(1, m):
        for j in range(1, n):
            if obstacleGrid[i][j] == 0:
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
            else:
                dp[i][j] = 0 # Obstacle blocks the path

    # Return the number of paths to the bottom-right cell.
    return dp[m-1][n-1]

More from this blog

C

Chatmagic blog

2894 posts