Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion graphs/minimum_effort_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import heapq

def min_effort_path(heights):
""" Given a 2D array of heights, write a function to return
the path with minimum effort.
Expand All @@ -15,4 +17,27 @@ def min_effort_path(heights):
int
minimum effort required to navigate the path from (0, 0) to heights[rows - 1][columns - 1]
"""
pass
if not heights:
return 0

rows, columns = len(heights), len(heights[0])

distances = [[float("inf")] * columns for row in range(rows)]

distances[0][0] = 0
priority_queue = [(0, 0, 0)]
visited = set()

while priority_queue:
dist, row, col = heapq.heappop(priority_queue)
if (row, col) == (rows-1, columns -1):
return dist
visited.add((row, col))
neighbors = [(row-1, col), (row+1, col), (row, col-1), (row, col+1)]
for neighbor in neighbors:
neighbor_row, neighbor_col = neighbor
if (0 <= neighbor_row < rows and 0 <=neighbor_col < columns) and neighbor not in visited:
new_dist = max(dist, abs(heights[row][col] - heights[neighbor_row][neighbor_col]))
if distances[neighbor_row][neighbor_col] > new_dist:
distances[neighbor_row][neighbor_col] = new_dist
heapq.heappush(priority_queue, (max(dist, new_dist), neighbor_row, neighbor_col))