-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpath_with_min_effort.py
26 lines (20 loc) · 989 Bytes
/
path_with_min_effort.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
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
def withinBounds(x, y, rowLen, colLen, visited):
return x >= 0 and x < rowLen and y >= 0 and y < colLen and (x, y) not in visited
heap = []
heap.append((0, 0, 0)) # (cost, row, col)
visited = set()
rowLen, colLen = len(heights), len(heights[0])
while heap:
cost, row, col = heappop(heap)
if row == rowLen - 1 and col == colLen - 1:
return cost
if (row, col) in visited:
continue
visited.add((row, col))
for neighbor in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
x = row + neighbor[0]
y = col + neighbor[1]
if withinBounds(x, y, rowLen, colLen, visited):
heappush(heap, (max(cost, abs(heights[x][y] - heights[row][col])), x, y))