-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanswer.py
38 lines (32 loc) · 1.24 KB
/
answer.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
#!/usr/bin/python3
#------------------------------------------------------------------------------
class Solution(object):
def uniquePathsWithObstacles(self, grid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not grid or not grid[0] or grid[0][0] == 1:
return 0
# **NOTE** Cannot use multiplication because it copies references
# Must use list comprehension for this to create cache
# cache = [[0]*len(grid[0])]*len(grid)
cache = [[0]*len(grid[0]) for _ in range(len(grid))]
cache[0][0] = 1
# Set side paths
for row in range(1, len(grid)):
if grid[row][0] == 1:
break
cache[row][0] = 1
for col in range(1, len(grid[0])):
if grid[0][col] == 1:
break
cache[0][col] = 1
# Set rest of cache
for row in range(1, len(grid)):
for col in range(1, len(grid[0])):
if grid[row][col] == 0:
cache[row][col] = cache[row-1][col] + cache[row][col-1]
return cache[-1][-1]
#------------------------------------------------------------------------------
#Testing