-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1631-path-with-minimum-effort.cpp
More file actions
35 lines (35 loc) · 1.24 KB
/
1631-path-with-minimum-effort.cpp
File metadata and controls
35 lines (35 loc) · 1.24 KB
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
class Solution {
public:
int minimumEffortPath(vector<vector<int>>& heights) {
int n = heights.size(), m = heights[0].size();
vector<pair<int, int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<vector<bool>> seen = vector<vector<bool>>(n, vector<bool>(m));
function<bool(int, int, int)> dfs = [&](int x, int y, int bound) {
if (x == n - 1 && y == m - 1) {
return true;
}
seen[x][y] = true;
for (auto [dx, dy]: directions) {
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= n || ny < 0 || ny >= m || seen[nx][ny]) continue;
int d = abs(heights[x][y] - heights[nx][ny]);
if (d <= bound && dfs(nx, ny, bound)) {
return true;
}
}
return false;
};
int left = 0, right = 1e9, ans = 1e9;
while (left <= right) {
int mid = left + (right - left)/2;
seen.assign(n, vector<bool>(m, false));
if (dfs(0, 0, mid)) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
};