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

submission #60

Open
wants to merge 1 commit 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
32 changes: 31 additions & 1 deletion graphs/minimum_effort_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,34 @@ 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

m, n = len(heights), len(heights[0])
DIR = [0, 1, 0, -1, 0]

def dfs(r, c, visited, threadshold):
if r == m-1 and c == n-1:
return True
visited[r][c] = True
for i in range(4):
nr, nc = r+DIR[i], c+DIR[i+1]
if nr < 0 or nr == m or nc < 0 or nc == n or visited[nr][nc]: continue
if abs(heights[nr][nc]-heights[r][c]) <= threadshold and dfs(nr, nc, visited, threadshold):
return True
return False

def can_reach_destination(threadshold):
visited = [[False] * n for _ in range(m)]
return dfs(0, 0, visited, threadshold)

left = 0
result = right = 10**6
while left <= right:
mid = left + (right-left) // 2
if can_reach_destination(mid):
right = mid - 1
result = mid
else:
left = mid + 1
return result