MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / Solution

Class Solution

DP/UniquePathII.py:82–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

80
81
82class Solution(object):
83
84 def uniquePathsWithObstacles(self, _map):
85 """
86 :type obstacleGrid: List[List[int]]
87 :rtype: int
88 """
89
90 if _map[0][0] == 1:
91 return 0
92
93 if _map[-1][-1] == 1:
94 return 0
95
96
97 n, m = len(_map), len(_map[0])
98
99 for i in range(n):
100 for j in range(m):
101 if _map[i][j] == 1:
102 _map[i][j] = 'x'
103 continue
104
105 x = _map[i-1][j] if i - 1 >= 0 and _map[i-1][j] != 'x' else 0
106 y = _map[i][j-1] if j - 1 >= 0 and _map[i][j-1] != 'x' else 0
107
108 if x + y != 0:
109 _map[i][j] = x + y
110 elif i + j == 0:
111 _map[i][j] = 1
112
113 return _map[n-1][m-1]
114
115 # def uniquePathsWithObstacles(self, _map):
116 # """
117 # :type obstacleGrid: List[List[int]]
118 # :rtype: int
119 # """
120
121 # if _map[0][0] == 1:
122 # return 0
123 # if _map[-1][-1] == 1:
124 # return 0
125
126 # for i, d in enumerate(_map):
127 # for j, d2 in enumerate(d):
128 # if d2 == 1:
129 # _map[i][j] = 'x'
130
131 # n, m = len(_map), len(_map[0])
132
133 # for i in range(n):
134 # for j in range(m):
135 # if _map[i][j] == 'x':
136 # continue
137 # x = _map[i-1][j] if i - 1 >= 0 and _map[i-1][j] != 'x' else 0
138 # y = _map[i][j-1] if j - 1 >= 0 and _map[i][j-1] != 'x' else 0
139

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected