From a5bc083a6f7f37c3cb8e4b7b4c6a25927cc4d50c Mon Sep 17 00:00:00 2001 From: Jamie H Date: Fri, 20 Jan 2023 23:42:35 -0500 Subject: [PATCH] finished --- graphs/minimum_effort_path.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/graphs/minimum_effort_path.py b/graphs/minimum_effort_path.py index 74fe13b..5e06285 100644 --- a/graphs/minimum_effort_path.py +++ b/graphs/minimum_effort_path.py @@ -1,3 +1,7 @@ +from collections import defaultdict +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 +19,33 @@ def min_effort_path(heights): int minimum effort required to navigate the path from (0, 0) to heights[rows - 1][columns - 1] """ - pass + graph = defaultdict(list) + if not heights: + return 0 + directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] + rows = len(heights) + cols = len(heights[0]) + + for x in range(cols): + for y in range(rows): + for direction in directions: + newx, newy = x + direction[0], y + direction[1] + if newx >= 0 and newx < cols and newy >= 0 and newy < rows: + abs_difference = abs(heights[y][x] - heights[newy][newx]) + graph[(y,x)].append(((newy, newx), abs_difference)) + visited = set() + min_heap = [] + start = (0,0) + end = (rows-1, cols-1) + min_heap.append((0, start)) + max_abs = 0 + while min_heap: + cost_till, curr, = heapq.heappop(min_heap) + visited.add(curr) + max_abs = max(max_abs, cost_till) + if curr == end: + return max_abs + for next, to_reach in graph[curr]: + if next not in visited: + heapq.heappush(min_heap, (to_reach, next)) + return max_abs