Skip to content

Commit 0a8f7e4

Browse files
authored
Update shortest-path-in-a-grid-with-obstacles-elimination.py
1 parent 2bd875d commit 0a8f7e4

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Python/shortest-path-in-a-grid-with-obstacles-elimination.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Time: O(m * n * k)
2-
# Space: O(m * n * k)
2+
# Space: O(m * n)
33

44
class Solution(object):
55
def shortestPath(self, grid, k):
@@ -18,17 +18,17 @@ def g(a, b):
1818
def a_star(grid, b, t, k):
1919
f, dh = g(b, t), 2
2020
closer, detour = [(b, k)], []
21-
lookup = set()
21+
lookup = {}
2222
while closer or detour:
2323
if not closer:
2424
f += dh
2525
closer, detour = detour, closer
2626
b, k = closer.pop()
2727
if b == t:
2828
return f
29-
if (b, k) in lookup:
29+
if b in lookup and lookup[b] >= k:
3030
continue
31-
lookup.add((b, k))
31+
lookup[b] = k
3232
for dx, dy in directions:
3333
nb = (b[0]+dx, b[1]+dy)
3434
if not (0 <= nb[0] < len(grid) and 0 <= nb[1] < len(grid[0]) and

0 commit comments

Comments
 (0)