forked from cmhungsteve/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacific-atlantic-water-flow.cpp
50 lines (40 loc) · 1.75 KB
/
pacific-atlantic-water-flow.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
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
vector<pair<int, int>> pacificAtlantic(vector<vector<int>>& matrix) {
if (matrix.empty()) {
return {};
}
vector<pair<int, int>> res;
const auto m = matrix.size(), n = matrix[0].size();
vector<vector<int>> visited(m, vector<int>(n));
for (int i = 0; i < m; ++i) {
pacificAtlanticHelper(matrix, i, 0, numeric_limits<int>::min(), PACIFIC, &visited, &res);
pacificAtlanticHelper(matrix, i, n - 1, numeric_limits<int>::min(), ATLANTIC, &visited, &res);
}
for (int j = 0; j < n; ++j) {
pacificAtlanticHelper(matrix, 0, j, numeric_limits<int>::min(), PACIFIC, &visited, &res);
pacificAtlanticHelper(matrix, m - 1, j, numeric_limits<int>::min(), ATLANTIC, &visited, &res);
}
return res;
}
private:
void pacificAtlanticHelper(const vector<vector<int>>& matrix, int x, int y, int prev_height, int prev_val,
vector<vector<int>> *visited, vector<pair<int, int>> *res) {
if (x < 0 || x >= matrix.size() ||
y < 0 || y >= matrix[0].size() ||
matrix[x][y] < prev_height || ((*visited)[x][y] | prev_val) == (*visited)[x][y]) {
return;
}
(*visited)[x][y] |= prev_val;
if ((*visited)[x][y] == (PACIFIC | ATLANTIC)) {
res->emplace_back(x, y);
}
for (const auto& dir : directions) {
pacificAtlanticHelper(matrix, x + dir.first, y + dir.second, matrix[x][y], (*visited)[x][y], visited, res);
}
}
enum ocean { PACIFIC = 1, ATLANTIC = 2 };
const vector<pair<int, int>> directions{ {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
};