-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0063_Unique_Paths_II.py
40 lines (35 loc) · 1.26 KB
/
0063_Unique_Paths_II.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
m = len(obstacleGrid) # n row
n = len(obstacleGrid[0]) # n column
# first element update
if obstacleGrid[0][0] == 1:
return 0
else:
obstacleGrid[0][0] = -1
# first row update
for j in range(1, n):
if obstacleGrid[0][j] == 1:
obstacleGrid[0][j] = 0
else:
if obstacleGrid[0][j-1] == 0:
obstacleGrid[0][j] = 0
else:
obstacleGrid[0][j] = -1
# first column update
for i in range(1, m):
if obstacleGrid[i][0] == 1:
obstacleGrid[i][0] = 0
else:
if obstacleGrid[i-1][0] == 0:
obstacleGrid[i][0] = 0
else:
obstacleGrid[i][0] = -1
# rest of cells
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 1:
obstacleGrid[i][j] = 0
else:
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]
return obstacleGrid[m-1][n-1] * -1