-
Notifications
You must be signed in to change notification settings - Fork 43
/
unique-paths-ii.py
147 lines (139 loc) · 4.64 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/83154114
# IDEA : DP
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m, n = len(obstacleGrid), len(obstacleGrid[0])
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if obstacleGrid[i - 1][j - 1] == 1:
dp[i][j] = 0
else:
if i == j == 1:
dp[i][j] = 1
else:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[m][n]
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/83154114
# IDEA : DP
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m, n = len(obstacleGrid), len(obstacleGrid[0])
dp = [[0] * n for _ in range(m)]
if obstacleGrid[0][0] == 0:
dp[0][0] = 1
for i in range(m):
for j in range(n):
if obstacleGrid[i][j] == 1:
dp[i][j] = 0
else:
if i != 0:
dp[i][j] += dp[i - 1][j]
if j != 0:
dp[i][j] += dp[i][j - 1]
return dp[m - 1][n - 1]
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/83154114
# IDEA : DP
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m, n = len(obstacleGrid), len(obstacleGrid[0])
dp = [[0] * n for _ in range(m)]
if obstacleGrid[0][0] == 0:
dp[0][0] = 1
for i in range(m):
for j in range(n):
if obstacleGrid[i][j] == 0:
if i == j == 0:
continue
else:
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[m - 1][n - 1]
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/83154114
# IDEA : MEMORY SEARCH
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m, n = len(obstacleGrid), len(obstacleGrid[0])
memo = [[0] * n for _ in range(m)]
return self.dfs(m - 1, n - 1, obstacleGrid, memo)
def dfs(self, m, n, obstacleGrid, memo): # methods of postion m, n
if obstacleGrid[m][n] == 1:
return 0
if m < 0 or n < 0:
return 0
if m == n == 0:
return 1
if memo[m][n]:
return memo[m][n]
up = self.dfs(m - 1, n, obstacleGrid, memo)
left = self.dfs(m, n - 1, obstacleGrid, memo)
memo[m][n] = up + left
return memo[m][n]
# V2
# https://blog.csdn.net/XX_123_1_RJ/article/details/81583698
# IDEA : DP
# idea :
# dp[i][j] = 0 # if grid[i][j] == 1
# dp[i][j] = dp[i-1][j] + dp[i][j-1] # if grid[i][j] == 0
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid):
n, m = len(obstacleGrid), len(obstacleGrid[0])
dp = [[0]*m for _ in range(n)] # initize dp
dp[0][0] = 1 if obstacleGrid[0][0] == 0 else 0
for i in range(n):
for j in range(m):
if obstacleGrid[i][j] == 0:
if i + 1 < n: # update the grid below
dp[i + 1][j] += dp[i][j]
if j + 1 < m: # update the grid at right hand side
dp[i][j + 1] += dp[i][j]
else: # if there is obstacle, mark it as 0
dp[i][j] = 0
return dp[n-1][m-1]
# if __name__ == '__main__':
# obstacleGrid = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
# solu =Solution()
# print(solu.uniquePathsWithObstacles(obstacleGrid))
# V3
# Time: O(m * n)
# Space: O(m + n)
class Solution(object):
# @param obstacleGrid, a list of lists of integers
# @return an integer
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
m, n = len(obstacleGrid), len(obstacleGrid[0])
ways = [0]*n
ways[0] = 1
for i in range(m):
if obstacleGrid[i][0] == 1:
ways[0] = 0
for j in range(n):
if obstacleGrid[i][j] == 1:
ways[j] = 0
elif j>0:
ways[j] += ways[j-1]
return ways[-1]