-
Notifications
You must be signed in to change notification settings - Fork 3
/
417.cpp
136 lines (125 loc) · 4.54 KB
/
417.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
129
130
131
132
133
134
135
136
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int M;
int N;
bool isValid(int x, int y) {
if (x < 0 || x >= M || y < 0 || y >= N) return false;
return true;
}
void bfs(queue<pair<int, int>>& q, vector<vector<bool>>& matrix, vector<vector<int>>& heights) {
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto& direction : directions) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (!isValid(nextX, nextY)) continue;
if (matrix[nextX][nextY]) continue;
if (heights[nextX][nextY] < heights[x][y]) continue;
q.push({nextX, nextY});
matrix[nextX][nextY] = true;
}
}
}
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
M = heights.size();
N = heights[0].size();
// pacific
vector<vector<bool>> pacific(M, vector<bool>(N, false));
queue<pair<int, int>> q;
for (int i = 0; i < M; ++i) {
q.push({i, 0});
pacific[i][0] = true;
}
for (int j = 0; j < N; ++j) {
q.push({0, j});
pacific[0][j] = true;
}
bfs(q, pacific, heights);
// atlantic
vector<vector<bool>> atlantic(M, vector<bool>(N, false));
for (int i = 0; i < M; ++i) {
q.push({i, N - 1});
atlantic[i][N - 1] = true;
}
for (int j = 0; j < N; ++j) {
q.push({M - 1, j});
atlantic[M - 1][j] = true;
}
bfs(q, atlantic, heights);
// combine
vector<vector<int>> res;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (pacific[i][j] && atlantic[i][j]) res.push_back({i, j});
}
}
return res;
}
};
// V2
// class Solution {
// public:
// vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
// int m = heights.size();
// int n = heights[0].size();
// vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
// // pacific
// vector<vector<bool>> canReachPacific(m, vector<bool>(n, false));
// queue<pair<int, int>> q;
// for (int i = 0; i < m; i++) {
// q.push(make_pair(i, 0));
// canReachPacific[i][0] = true;
// }
// for (int j = 1; j < n; j++) {
// q.push(make_pair(0, j));
// canReachPacific[0][j] = true;
// }
// while (!q.empty()) {
// auto [x, y] = q.front();
// q.pop();
// for (auto direction : directions) {
// int _x = x + direction.first;
// int _y = y + direction.second;
// if (_x < 0 || _x >= m || _y < 0 || _y >= n) continue;
// if (canReachPacific[_x][_y]) continue;
// if (heights[x][y] > heights[_x][_y]) continue;
// q.push(make_pair(_x, _y));
// canReachPacific[_x][_y] = true;
// }
// }
// while (!q.empty()) q.pop();
// // atlantic
// vector<vector<bool>> canReachAtlantic(m, vector<bool>(n, false));
// for (int i = 0; i < m; i++) {
// q.push(make_pair(i, n - 1));
// canReachAtlantic[i][n - 1] = true;
// }
// for (int j = 0; j < n - 1; j++) {
// q.push(make_pair(m - 1, j));
// canReachAtlantic[m - 1][j] = true;
// }
// while (!q.empty()) {
// auto [x, y] = q.front();
// q.pop();
// for (auto direction : directions) {
// int _x = x + direction.first;
// int _y = y + direction.second;
// if (_x < 0 || _x >= m || _y < 0 || _y >= n) continue;
// if (canReachAtlantic[_x][_y]) continue;
// if (heights[x][y] > heights[_x][_y]) continue;
// q.push(make_pair(_x, _y));
// canReachAtlantic[_x][_y] = true;
// }
// }
// // final
// vector<vector<int>> res;
// for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// if (canReachPacific[i][j] && canReachAtlantic[i][j]) res.push_back({i, j});
// }
// }
// return res;
// }
// };