Участник:Timplech/UniquePaths2

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/unique-paths-ii/

Python3

 
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        if not obstacleGrid:  
            return 0
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        mem = list([0 for _ in range(0, n)] for _ in range(0, m))
        for i in range(0, m):
            for j in range(0, n):
                if obstacleGrid[i][j] == 1:
                    continue
                if i > 0 and j > 0:
                    mem[i][j] = mem[i-1][j] + mem[i][j-1]
                elif i:
                    mem[i][j] = mem[i-1][j]
                elif j:
                    mem[i][j] = mem[i][j-1]
                else:
                    mem[i][j] = 1
        return mem[m-1][n-1]