-
Notifications
You must be signed in to change notification settings - Fork 0
/
1631. Path With Minimum Effort.cpp
57 lines (45 loc) · 1.74 KB
/
1631. 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
57
// https://leetcode.com/problems/path-with-minimum-effort/
class Solution
{
public:
int minimumEffortPath(vector<vector<int>> &heights)
{
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> minHeap;
vector<vector<int>> efforts(heights.size(), vector<int>(heights[0].size(), INT_MAX));
vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
minHeap.push({0, 0, 0});
while (!minHeap.empty())
{
vector<int> current = minHeap.top();
minHeap.pop();
int effort = current[0];
int row = current[1];
int column = current[2];
if (effort > efforts[row][column])
continue;
if (row == heights.size() - 1 && column == heights[row].size() - 1)
return effort;
for (vector<int> &direction : directions)
{
int newRow = row + direction[0];
int newColumn = column + direction[1];
if (isValidPosition(heights, newRow, newColumn))
{
int maxEffort = max(effort, abs(heights[row][column] - heights[newRow][newColumn]));
if (maxEffort < efforts[newRow][newColumn])
{
efforts[newRow][newColumn] = maxEffort;
minHeap.push({maxEffort, newRow, newColumn});
}
}
}
}
return efforts[heights.size() - 1][heights[0].size() - 1];
}
bool isValidPosition(vector<vector<int>> &heights, int i, int j)
{
if (i >= heights.size() || i < 0 || j >= heights[i].size() || j < 0)
return false;
return true;
}
};