-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path59_pacific-atlantic-water-flow.cpp
69 lines (65 loc) · 2.21 KB
/
59_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// cpp-blind-75/59_pacific-atlantic-water-flow.cpp
/**
* Date : 12-Aug-2023
* Repo: https://github.com/ankitsamaddar/
*
* Problem : Pacific Atlantic Water Flow
* Difficulty: 🟡Medium
*
* Leetcode 0417 : https://leetcode.com/problems/pacific-atlantic-water-flow
* Lintcode 0778 : https://www.lintcode.com/problem/778
*/
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
rows = heights.size();
cols = heights[0].size();
// set to keep track of cells that can reach Pacific/Atlantic
unordered_set<int> pac;
unordered_set<int> atl;
// Hash the rows
for (int c = 0; c < cols; c++) {
int prevHeight = heights[0][c];
dfs(0, c, pac, prevHeight, heights); // Pacific - first row
dfs(rows - 1, c, atl, heights[rows - 1][c], heights); // Atlantic - last row
}
// Hash the columns
for (int r = 0; r < rows; r++) {
dfs(r, 0, pac, heights[r][0], heights); // Pacific - first column
dfs(r, cols - 1, atl, heights[r][cols - 1], heights); // Atlantic - last column
}
vector<vector<int>> res;
// Iterate the matrix and store the cells that can reach both ocean
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (
// check if cell can reach Pacific(does exist)
pac.find(r * cols + c) != pac.end() &&
// check if cell can reach Atlantic
atl.find(r * cols + c) != atl.end()) {
res.push_back({r, c});
}
}
}
return res;
}
private:
int rows, cols;
void dfs(int r, int c, unordered_set<int>& visit, int prevHeight, vector<vector<int>>& heights) {
if (
// Already visited
(visit.count(r * cols + c)) ||
// row and col within limits
r < 0 || c < 0 || r == rows || c == cols ||
// height is less than the side
heights[r][c] < prevHeight) {
return;
}
visit.insert(r * cols + c);
// dfs in every direction
dfs(r + 1, c, visit, heights[r][c], heights);
dfs(r - 1, c, visit, heights[r][c], heights);
dfs(r, c + 1, visit, heights[r][c], heights);
dfs(r, c - 1, visit, heights[r][c], heights);
}
};