Clear Python DP solution


  • 0
    L
    class Solution(object):
        def uniquePathsWithObstacles(self, obstacleGrid):
            m = len(obstacleGrid)
            n = len(obstacleGrid[0])
            dp = [[0 for col in range(n)] for row in range(m)]
            for i in range(0, m):
                for j in range(0, n):
                    if obstacleGrid[i][j] == 1:
                        dp[i][j] = 0
                    elif i == 0 or j == 0:
                        if i:
                            dp[i][j] = dp[i-1][j]
                        elif j:
                            dp[i][j] = dp[i][j-1]
                        else:
                            dp[i][j] = 1
                    else:
                        dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
            return dp[-1][-1]

Log in to reply
 

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