menu

[LeetCode] 063. Unique Path II

Problem (Medium)

063. Unique Path II

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Note: m and n will be at most 100.

Example 1:

Input: [ [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

Approach 1: (My Solution - DP)

Idea

Same idea as the previous problem, using DP, add a verification during the loops.

Solution

class Solution1:
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        n, m = len(obstacleGrid), len(obstacleGrid[0])
        dp = [[0 for _ in range(m)] for _ in range(n)]
        for i in range(n):
            dp[i][0] = 1 - obstacleGrid[i][0]
            if obstacleGrid[i][0]:
                break
        for j in range(m):
            dp[0][j] = 1 - obstacleGrid[0][j]
            if obstacleGrid[0][j]:
                break

        print('obstacleGrid:', obstacleGrid)
        print('original dp:', dp)
        #if obstacleGrid[0][0]:
        #    return 0

        for i in range(1, n):
            for j in range(1, m):
                if obstacleGrid[i][j]:
                    continue
                dp[i][j] = (dp[i][j-1] + dp[i-1][j])
        print('end dp:', dp)
        return dp[-1][-1]

Complexity

  • Time: $O(mn)$
  • Space: $O(mn)$

Approach 2: (My Solution - DP Improved)

Idea

Only use the given obstacleGrid instead of a new 2-D matrix. Thus the space complexity is reduced to $O(1)$.

Solution

class Solution2:
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        n, m = len(obstacleGrid), len(obstacleGrid[0])
        dp = [[0 for _ in range(m)] for _ in range(n)]
        for i in range(n):
            dp[i][0] = 1 - obstacleGrid[i][0]
            if obstacleGrid[i][0]:
                break
        for j in range(m):
            dp[0][j] = 1 - obstacleGrid[0][j]
            if obstacleGrid[0][j]:
                break

        print('obstacleGrid:', obstacleGrid)
        print('original dp:', dp)
        #if obstacleGrid[0][0]:
        #    return 0

        for i in range(1, n):
            for j in range(1, m):
                if obstacleGrid[i][j]:
                    continue
                dp[i][j] = (dp[i][j-1] + dp[i-1][j])
        print('end dp:', dp)
        return dp[-1][-1]

Complexity

  • Time: $O(mn)$
  • Space: $O(1)$



KF

Comments