simple and in-place dp using python


  • 0
    W

    simple and in-place dp using python

        def uniquePathsWithObstacles(self, obstacleGrid):
            """
            :type obstacleGrid: List[List[int]]
            :rtype: int
            """
            n = len(obstacleGrid)
            if n == 0:
                return 0
            m = len(obstacleGrid[0])
            if m == 0:
                return 0
            
            for i in range(0, n):
                for j in range(m):
                    if obstacleGrid[i][j] == 1:
                        obstacleGrid[i][j] = 0
                        continue
    
                    if i-1 >= 0 and j -1 >= 0:
                        obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]
                    elif i - 1 >= 0:
                        obstacleGrid[i][j] = obstacleGrid[i-1][j]
                    elif j - 1 >= 0:
                        obstacleGrid[i][j] = obstacleGrid[i][j-1]
                    else:
                        obstacleGrid[i][j] = 1
                    
            return obstacleGrid[n-1][m-1]
    

Log in to reply
 

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