From 5d524c8da0ca79a11e9bdcef1fc005d132f6b8dd Mon Sep 17 00:00:00 2001 From: Jae Date: Mon, 16 Jan 2023 01:17:54 -0500 Subject: [PATCH 1/2] passingtests --- graphs/minimum_effort_path.py | 73 ++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/graphs/minimum_effort_path.py b/graphs/minimum_effort_path.py index 74fe13b..57f3844 100644 --- a/graphs/minimum_effort_path.py +++ b/graphs/minimum_effort_path.py @@ -1,3 +1,5 @@ +from heapq import heappush, heappop + def min_effort_path(heights): """ Given a 2D array of heights, write a function to return the path with minimum effort. @@ -15,4 +17,73 @@ def min_effort_path(heights): int minimum effort required to navigate the path from (0, 0) to heights[rows - 1][columns - 1] """ - pass + if heights is None: + return 0 + + # set target to last elem in array + max_x = len(heights)-1 + max_y = len(heights[0])-1 + target = (max_x, max_y) + + # initialize queue to check each node in input + priority_queue = [(0, (0,0))] + + #initialize distance: track distance from start -> each node + distance = { + # from 0,0 to itself = 0 + (0,0): 0 + } + + directions = [ + (1, 0), #down + (0, 1), # right + (-1, 0), # up + (0, -1) #left + ] + + while priority_queue: + # in each iteration, remove first elem + cost, node = heappop(priority_queue) + # set current node to first in priority_queue + current_x, current_y = node + + if node == target: + break + + + for direction in directions: + # loop through directions to check neighbors (when valid) + # find the shortest distance from current_node to neighbors + new_x, new_y = current_x + direction[0], current_y + direction[1] + if max_x >= new_x >= 0 <= new_y <= max_y: + # current node + current_node = heights[current_x][current_y] + # each valid neighbor for current + current_neighbor = heights[new_x][new_y] + + + # get abs val of distance between current node and neighbor + # in each directiion (by looping through directions) + distance_between = abs(current_node - current_neighbor) + edge_cost = max(cost, distance_between) + + + if (new_x, new_y) not in distance or ((new_x, new_y) in distance + and edge_cost < distance[new_x, new_y]): + #find the best edge_cost between current node and + # next node in all valid directions, add that to distance dict + distance[(new_x, new_y)] = edge_cost + print(distance) + # push new coordinates to check in all directions + heappush(priority_queue, (edge_cost, (new_x, new_y))) + # print(distance) + return distance[target] + + +# heights = [ +# [1,2,2], +# [3,8,2], +# [5,3,5]] + +# min_effort_path(heights) + From 89b825f41271c9a2c656839068153d667abb269b Mon Sep 17 00:00:00 2001 From: Jae Date: Mon, 16 Jan 2023 01:20:24 -0500 Subject: [PATCH 2/2] reformat --- graphs/minimum_effort_path.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/graphs/minimum_effort_path.py b/graphs/minimum_effort_path.py index 57f3844..9586e4f 100644 --- a/graphs/minimum_effort_path.py +++ b/graphs/minimum_effort_path.py @@ -1,23 +1,27 @@ from heapq import heappush, heappop def min_effort_path(heights): - """ Given a 2D array of heights, write a function to return + + """ + Given a 2D array of heights, write a function to return the path with minimum effort. A route's effort is the maximum absolute difference in heights between two consecutive cells of the route. - Parameters - ---------- - heights : list[list[]] (2D array) - 2D array containing the heights of the available paths + Parameters + ---------- + heights : list[list[]] (2D array) + 2D array containing the heights of the available paths + + Returns + ------- + int + minimum effort required to navigate the path from (0, 0) + to heights[rows - 1][columns - 1] - Returns - ------- - int - minimum effort required to navigate the path from (0, 0) to heights[rows - 1][columns - 1] """ - if heights is None: + if heights is None: return 0 # set target to last elem in array