-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path63.unique-paths-ii.py
66 lines (48 loc) · 1.56 KB
/
63.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# @lc app=leetcode id=63 lang=python3
#
# [63] Unique Paths II
#
# @lc code=start
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
# make the cols and rows 0
rs = len(obstacleGrid)
if rs == 0:
return 0
if rs == 1:
booleans = map (lambda n : n == 1, obstacleGrid[0])
# if there's 0's output 1
# if any 1 output 0
if any(booleans):
return 0
else:
return 1
cs = len(obstacleGrid[0])
for r in range(rs):
for c in range(cs):
val = obstacleGrid[r][c]
if val == 1:
obstacleGrid[r][c] = -1
for r in range(rs):
obstacleGrid[r][0] = 1
for c in range(cs):
obstacleGrid[0][c] = 1
print(obstacleGrid)
for r in range(1, rs):
for c in range(1, cs):
val = obstacleGrid[r][c]
# Skip if 1 as it is an obstacle
if val != -1:
top = obstacleGrid[r-1][c]
top = top if top != -1 else 0
left = obstacleGrid[r][c-1]
left = left if left != -1 else 0
obstacleGrid[r][c] = top + left
return 0 if obstacleGrid[-1][-1] == -1 else obstacleGrid[-1][-1]
"""
[[0],[1]] --> 0
"""
# if __name__ == "__main__":
# Solution().uniquePathsWithObstacles([[0,0,0],[0,1,0],[0,0,0]])
# @lc code=end