-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_063_UniquePaths2.py
31 lines (24 loc) · 1.14 KB
/
_063_UniquePaths2.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
#-----------------------------------------------------------------------------
# Runtime: 32ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: [[int]]) -> int:
if obstacleGrid[0][0] == 1: return 0
row_lenght = len(obstacleGrid)
column_lenght = len(obstacleGrid[0])
if row_lenght <= column_lenght:
possiblePath = [0] * row_lenght
possiblePath[0] = 1
for i in range(column_lenght):
for j in range(row_lenght):
possiblePath[j] = 0 if obstacleGrid[j][i] == 1 else (possiblePath[j] if j == 0 else possiblePath[j - 1] + possiblePath[j])
return possiblePath[-1]
else:
possiblePath = [0] * column_lenght
possiblePath[0] = 1
for i in range(row_lenght):
for j in range(column_lenght):
possiblePath[j] = 0 if obstacleGrid[i][j] == 1 else (possiblePath[j] if j == 0 else possiblePath[j - 1] + possiblePath[j])
return possiblePath[-1]