My Simple Python version


  • 0
    A
    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            n_rows = len(obstacleGrid)
            n_column = len(obstacleGrid[0])
            dp = [0] * n_column
            dp[0] = 1
            for i, row in enumerate(obstacleGrid):
                for j, column in enumerate(obstacleGrid[i]):
                    if obstacleGrid[i][j] == 1:
                        dp[j] = 0
                    elif j > 0:
                        dp[j] += dp[j - 1]
            
            return dp[n_column-1]
    

    dp[x] is the paths number to column x in the current row.


  • 0
    A
    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            n_rows = len(obstacleGrid)
            n_column = len(obstacleGrid[0])
            dp = [0] * n_column
            dp[0] = 1
            for i, row in enumerate(obstacleGrid):
                for j, column in enumerate(row):
                    if column == 1:
                        dp[j] = 0
                    elif j > 0:
                        dp[j] += dp[j - 1]
            
            return dp[n_column-1]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.