Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C17- Chinazo Onwukaike #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
23 changes: 22 additions & 1 deletion graphs/minimum_effort_path.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
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,23 @@ 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 = len(heights)
cols = len(heights[0])
vis = [[False for i in range(cols)] for j in range(rows)]
q = []
heapq.heappush(q, (0,0,0))
while len(q) > 0:
cur = heapq.heappop(q)
if vis[cur[1]][cur[2]]:
continue
if cur[1] == rows-1 and cur[2] == cols-1:
return cur[0]
vis[cur[1]][cur[2]] = True
for row, col in [(-1,0),(1,0),(0,-1),(0,1)]:
next_row, next_col = cur[1]+row, cur[2]+col
if 0<=next_row<rows and 0<=next_col<cols and not vis[next_row][next_col]:
heapq.heappush(q, (max(cur[0], abs(heights[next_row][next_col]-heights[cur[1]][cur[2]])), next_row, next_col))
return -1