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

Esther Annorzie - C17 - Section A #77

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 30 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,31 @@ 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])
visited = set()

min_eff = 0
start = [(0, 0, 0)]
dirs = [(-1, 0), (0, 1), (0, -1), (1, 0)]

while start:
eff, row, col = heapq.heappop(start)
visited.add((row, col))
min_eff = max(min_eff, eff)

if row == rows - 1 and col == cols - 1:
return min_eff

for x_coor, y_coor in dirs:
next_row = row + x_coor
next_col = col + y_coor

if rows > next_row >= 0 <= next_col < cols:
if (next_row, next_col) not in visited:
heapq.heappush(start, (abs(heights[row][col] - heights[next_row][next_col]), next_row, next_col))

return min_eff
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ packaging==20.8
pluggy==0.13.1
py==1.10.0
pyparsing==2.4.7
pytest==6.2.1
pytest==6.2.5
toml==0.10.2