1631-path-with-minimum-effort¶
Try it on leetcode
Description¶
You are a hiker preparing for an upcoming hike. You are given heights
, a 2D array of size rows x columns
, where heights[row][col]
represents the height of cell (row, col)
. You are situated in the top-left cell, (0, 0)
, and you hope to travel to the bottom-right cell, (rows-1, columns-1)
(i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort.
A route's effort is the maximum absolute difference in heights between two consecutive cells of the route.
Return the minimum effort required to travel from the top-left cell to the bottom-right cell.
Example 1:
Input: heights = [[1,2,2],[3,8,2],[5,3,5]] Output: 2 Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells. This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
Example 2:
Input: heights = [[1,2,3],[3,8,4],[5,3,5]] Output: 1 Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
Example 3:

Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]] Output: 0 Explanation: This route does not require any effort.
Constraints:
rows == heights.length
columns == heights[i].length
1 <= rows, columns <= 100
1 <= heights[i][j] <= 106
Solution(Python)¶
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
return self.binarySearch(heights)
# Time Complexity: O(ElogV) = O(M*N*log(M*N))
# Space Complexity: O(M*N)
def dijisktra(self, heights: List[List[int]]) -> int:
m, n = len(heights), len(heights[0])
dist = [[math.inf] * n for _ in range(m)]
dist[0][0] = 0
PQ = [(0, 0, 0)]
DIRS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
while PQ:
w, r, c = heappop(PQ)
if w > dist[r][c]:
continue
if r == m - 1 and c == n - 1:
return w
for dx, dy in DIRS:
nr, nc = r + dx, c + dy
if self.isvalid(nr, nc, m, n):
newDist = max(w, abs(heights[nr][nc] - heights[r][c]))
if dist[nr][nc] > newDist:
dist[nr][nc] = newDist
heappush(PQ, (dist[nr][nc], nr, nc))
def isvalid(self, x, y, m, n):
return 0 <= x < m and 0 <= y < n
# Time Complexity: O(M*N log(MaxHEight))
# Space Complexity: O(m*n)
def binarySearch(self, heights: List[List[int]]) -> int:
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 # Reach destination
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 canReachDestination(threadshold):
visited = [[False] * n for _ in range(m)]
return dfs(0, 0, visited, threadshold)
left = 0
ans = right = 10**6
while left <= right:
mid = left + (right - left) // 2
if canReachDestination(mid):
right = mid - 1 # Try to find better result on the left side
ans = mid
else:
left = mid + 1
return ans