-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path0417-pacific-atlantic-water-flow.cpp
128 lines (105 loc) · 3.97 KB
/
0417-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
Top & left pacific, bottom & right atlantic, determine spots that flow to both
Instead go outside in, from oceans to spots where rain could flow from
Faster bc avoids repeated work: cells along a path can also reach that ocean
Time: O(m x n)
Space: O(m x n)
*/
class Solution {
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
int m = heights.size();
int n = heights[0].size();
vector<vector<bool>> pacific(m, vector<bool>(n));
vector<vector<bool>> atlantic(m, vector<bool>(n));
for (int i = 0; i < m; i++) {
dfs(heights, pacific, i, 0, m, n);
dfs(heights, atlantic, i, n - 1, m, n);
}
for (int j = 0; j < n; j++) {
dfs(heights, pacific, 0, j, m, n);
dfs(heights, atlantic, m - 1, j, m, n);
}
vector<vector<int>> result;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (pacific[i][j] && atlantic[i][j]) {
result.push_back({i, j});
}
}
}
return result;
}
private:
void dfs(vector<vector<int>>& heights, vector<vector<bool>>& visited,
int i, int j, int m, int n) {
visited[i][j] = true;
if (i > 0 && !visited[i - 1][j] && heights[i - 1][j] >= heights[i][j]) {
dfs(heights, visited, i - 1, j, m, n);
}
if (i < m - 1 && !visited[i + 1][j] && heights[i + 1][j] >= heights[i][j]) {
dfs(heights, visited, i + 1, j, m, n);
}
if (j > 0 && !visited[i][j - 1] && heights[i][j - 1] >= heights[i][j]) {
dfs(heights, visited, i, j - 1, m, n);
}
if (j < n - 1 && !visited[i][j + 1] && heights[i][j + 1] >= heights[i][j]) {
dfs(heights, visited, i, j + 1, m, n);
}
}
};
/*
BFS solution
*/
class Solution {
private:
int rows, cols;
void bfs(queue<pair<int, int>>& q, vector<vector<bool>>& visited, vector<vector<int>>& heights) {
vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
while (!q.empty()) {
auto [row, col] = q.front();
q.pop();
for (const auto &direction : directions) {
int newRow = row + direction.first;
int newCol = col + direction.second;
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && !visited[newRow][newCol]
&& heights[newRow][newCol] >= heights[row][col]) {
q.push({newRow, newCol});
visited[newRow][newCol] = true;
}
}
}
}
public:
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
rows = heights.size();
cols = heights[0].size();
vector<vector<int>> results;
queue<pair<int, int>> pacificQueue;
queue<pair<int, int>> atlanticQueue;
vector<vector<bool>> pacific(rows, vector<bool>(cols, false));
vector<vector<bool>> atlantic(rows, vector<bool>(cols, false));
for (int row = 0; row < rows; ++row) {
pacific[row][0] = true;
atlantic[row][cols - 1] = true;
pacificQueue.push({row, 0});
atlanticQueue.push({row, cols-1});
}
for (int col = 0; col < cols; ++col) {
pacific[0][col] = true;
atlantic[rows - 1][col] = true;
pacificQueue.push({0, col});
atlanticQueue.push({rows - 1, col});
}
bfs(pacificQueue, pacific, heights);
bfs(atlanticQueue, atlantic, heights);
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < cols; ++col) {
if (pacific[row][col] && atlantic[row][col]) {
results.push_back({row, col});
}
}
}
return results;
}
};