19. Path With Minimum Effort
β GFG solution to the Path With Minimum Effort problem: find minimum cost path where cost is maximum absolute difference between consecutive cells using Dijkstra's algorithm. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a 2D array mat[][] of size n*m. Your task is to find the minimum possible path cost from the top-left cell (0, 0) to the bottom-right cell (n-1, m-1) by moving up, down, left, or right between adjacent cells.
Note: The cost of a path is defined as the maximum absolute difference between the values of any two consecutive cells along that path.
π Examples
Example 1
Input: mat[][] = [[7, 2, 6, 5],
[3, 1, 10, 8]]
Output: 4
Explanation: The route of [7, 3, 1, 2, 6, 5, 8] has a minimum value of maximum absolute
difference between any two consecutive cells in the route, i.e., 4.Example 2
Input: mat[][] = [[2, 2, 2, 1],
[8, 1, 2, 7],
[2, 2, 2, 8],
[2, 1, 4, 7],
[2, 2, 2, 2]]
Output: 0
Explanation: The route of [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] has a minimum value of
maximum absolute difference between any two consecutive cells in the route, i.e., 0.π Constraints
$1 \le n, m \le 100$
$0 \le \text{mat}[i][j] \le 10^6$
β
My Approach
The optimal approach uses Dijkstra's Algorithm with a Priority Queue to find the path with minimum effort:
Dijkstra's Algorithm with Modified Cost
Initialize Data Structures:
Create a
distarray initialized with infinity (1e9) to track minimum effort to reach each cell.Use a min-heap priority queue storing tuples of (effort, x-coordinate, y-coordinate).
Start from (0, 0) with effort 0.
Process Cells:
Pop the cell with minimum effort from priority queue.
If destination (n-1, m-1) is reached, return the effort.
Skip if current effort is greater than recorded distance (already processed optimally).
Explore Neighbors:
For each of 4 directions (up, down, left, right):
Calculate new effort as maximum of current effort and absolute difference with neighbor.
If new effort is less than recorded distance for neighbor, update and push to queue.
Cost Calculation:
Path cost = maximum absolute difference encountered along the entire path.
Update using:
new_effort = max(current_effort, abs(mat[next] - mat[current])).
Return Result:
Return the minimum effort recorded for destination cell.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n Γ m Γ log(n Γ m)), where n and m are the dimensions of the matrix. Each cell is processed at most once, and priority queue operations take O(log(n Γ m)) time. In the worst case, we may push each cell into the priority queue multiple times before finding the optimal path.
Expected Auxiliary Space Complexity: O(n Γ m), as we maintain a distance array of size n Γ m and the priority queue can hold at most O(n Γ m) elements in the worst case.
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated