-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cpp
61 lines (51 loc) · 1.61 KB
/
Solution.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
58
59
60
61
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
vector<pair<int, int>> const directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
int swimInWater(vector<vector<int>>& grid) {
// grid[i][j] represents the elevation at (i, j)
// At time t, the depth of the water everywhere is t.
// We can move to a neighboring cell if its elevation is at most t.
// The intuition is to traverse cells from lower to higher elevations,
// keeping track of the maximum elevation encountered.
// The time needed is the maximum elevation in the path to the destination.
int n = grid.size();
// Stores {elevation, row, col}
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
greater<tuple<int, int, int>>>
minHeap;
vector<vector<bool>> visited(n, vector<bool>(n, false));
visited[0][0] = true;
minHeap.emplace(grid[0][0], 0, 0);
int time = 0;
while (!minHeap.empty()) {
auto [elevation, r, c] = minHeap.top();
minHeap.pop();
time = max(time, elevation);
if (r == n - 1 && c == n - 1) {
break;
}
for (auto const& [dr, dc] : directions) {
int nr = r + dr;
int nc = c + dc;
if (nr < 0 || nc < 0 || nr >= n || nc >= n || visited[nr][nc]) {
continue;
}
visited[nr][nc] = true;
minHeap.emplace(grid[nr][nc], nr, nc);
}
}
return time;
}
};