-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPath With Minimum Effort.cpp
56 lines (37 loc) · 1.89 KB
/
Path With Minimum Effort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Solution by Rahul Surana
***********************************************************
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.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int m = heights.size(), n = heights[0].size();
int dirs[5] = {-1, 0, 1, 0, -1};
std::vector<vector<int>> efforts(m, vector<int>(n, INT_MAX));
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.emplace(0, 0); // First item is effort, second is row * 100 + col
while (!pq.empty()) {
int effort = pq.top().first;
int x = pq.top().second / 100, y = pq.top().second % 100;
pq.pop();
if (x == m - 1 && y == n - 1) return effort;
if (effort >= efforts[x][y]) continue;
efforts[x][y] = effort;
for (int i = 0; i < 4; i++) {
int nx = x + dirs[i], ny = y + dirs[i + 1];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
int n_effort = max(effort, abs(heights[x][y] - heights[nx][ny]));
pq.emplace(n_effort, nx * 100 + ny);
}
}
return -1;
}
};