diff --git a/graphs/minimum_effort_path.py b/graphs/minimum_effort_path.py index 74fe13b..82826e3 100644 --- a/graphs/minimum_effort_path.py +++ b/graphs/minimum_effort_path.py @@ -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. @@ -15,4 +17,35 @@ 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) + columns = len(heights[0]) + + pq = [[0,0,0]] ##diff,x,y + + visited = set() + directions = [[0, 1], [1, 0], [0, -1], [-1, 0]] + ans = float('-inf') + + while pq: + diff,x,y=heapq.heappop(pq) + ans=max(diff,ans) + if x==rows-1 and y==columns-1: + return ans + + if (x,y) in visited: + continue + + visited.add((x,y)) + + for dx,dy in directions: + new_x = x+dx + new_y = y+dy + + if new_x>=0 and new_x=0 and new_y