Skip to content

Commit 7cef6b0

Browse files
committed
Added unique path ii
1 parent 7435d6b commit 7cef6b0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

unique_paths_ii/solution.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
# @param obstacleGrid, a list of lists of integers
3+
# @return an integer
4+
def uniquePathsWithObstacles(self, obstacleGrid):
5+
n = len(obstacleGrid)
6+
m = len(obstacleGrid[0])
7+
for i in range(n):
8+
for j in range(m):
9+
if i == 0:
10+
if obstacleGrid[i][j] == 0:
11+
if j == 0 or (j > 0 and obstacleGrid[i][j - 1] != 0):
12+
obstacleGrid[i][j] = 1
13+
else:
14+
obstacleGrid[i][j] = 0
15+
elif j == 0:
16+
if obstacleGrid[i][j] == 0:
17+
# i must be greater that zero here
18+
if obstacleGrid[i - 1][j] != 0:
19+
obstacleGrid[i][j] = 1
20+
else:
21+
obstacleGrid[i][j] = 0
22+
else:
23+
if obstacleGrid[i][j] == 0:
24+
obstacleGrid[i][j] = (obstacleGrid[i - 1][j]
25+
+ obstacleGrid[i][j - 1])
26+
else:
27+
obstacleGrid[i][j] = 0
28+
return obstacleGrid[n - 1][m - 1]

0 commit comments

Comments
 (0)