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
34 changes: 34 additions & 0 deletions graphs/minimum_effort_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import deque
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 @@ -16,3 +18,35 @@ def min_effort_path(heights):
minimum effort required to navigate the path from (0, 0) to heights[rows - 1][columns - 1]
"""
pass
if heights == None:
return 0
H = len(heights)
W = len(heights[0])

directions = [[1,0], [0,1], [-1,0], [0,-1]]
def inside(row, col):
return 0 <= row < H and 0 <= col < W
if len(heights) >0:

queue = deque([])
queue.append((0, 0))

cost = { (row, col): float('inf') for col in range(W) for row in range(H) }
cost[(0, 0)] = 0

while queue:
row, col = queue.popleft()
current_height = heights[row][col]
current_cost = cost[(row, col)]
for d_row, d_col in directions:
new_row = row + d_row
new_col = col + d_col
if inside(new_row, new_col):
neighbor_height = heights[new_row][new_col]
new_cost = max(current_cost, abs(neighbor_height - current_height))
if new_cost < cost[(new_row, new_col)]:
cost[(new_row, new_col)] = new_cost
queue.append((new_row, new_col))

return cost[(H - 1, W - 1)]