-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique path II
13 lines (13 loc) · 976 Bytes
/
unique path II
1
2
3
4
5
6
7
8
9
10
11
12
13
def uniquePathsWithObstacles(self, obstacleGrid):
if len(obstacleGrid) == 0 or len(obstacleGrid[0]) == 0 or obstacleGrid[0][0] == 1 or obstacleGrid[len(obstacleGrid)-1][len(obstacleGrid[0])-1] == 1:
return 0
solution = [[abs(obstacleGrid[i][j]-1) for j in range(len(obstacleGrid[0]))] for i in range(len(obstacleGrid))]
for row in range(len(obstacleGrid)):
for col in range(len(obstacleGrid[0])):
if row == 0 and col > 0 and solution[row][col] != 0: #iff no obstacle
solution[row][col] = solution[row][col-1]
elif col == 0 and row > 0 and solution[row][col] != 0: #iff no obstacle
solution[row][col] = solution[row-1][col]
elif row > 0 and col > 0 and solution[row][col] != 0: #iff no obstacle
solution[row][col] = solution[row-1][col] + solution[row][col-1]
return solution[len(solution)-1][len(solution[0])-1]