-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0778-swim-in-rising-water.cpp
54 lines (41 loc) · 1.5 KB
/
0778-swim-in-rising-water.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
/*
Given an integer elevation matrix, rain falls, at time t, depth everywhere is t
Can swim iff elevation at most t, return least time get from top left to bottom right
Shortest path w/ min heap: at every step, find lowest water level to move forward
Time: O(n^2 log n)
Space: O(n^2)
*/
class Solution {
public:
int swimInWater(vector<vector<int>>& grid) {
int n = grid.size();
if (n == 1) {
return 0;
}
vector<vector<bool>> visited(n, vector<bool>(n));
visited[0][0] = true;
int result = max(grid[0][0], grid[n - 1][n - 1]);
priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;
pq.push({result, 0, 0});
while (!pq.empty()) {
vector<int> curr = pq.top();
pq.pop();
result = max(result, curr[0]);
for (int i = 0; i < 4; i++) {
int x = curr[1] + dirs[i][0];
int y = curr[2] + dirs[i][1];
if (x < 0 || x >= n || y < 0 || y >= n || visited[x][y]) {
continue;
}
if (x == n - 1 && y == n - 1) {
return result;
}
pq.push({grid[x][y], x, y});
visited[x][y] = true;
}
}
return -1;
}
private:
vector<vector<int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
};