Skip to content

Commit b0d13f4

Browse files
authored
feat: add solutions to lc problem: No.0417 (#4764)
1 parent 2f1bf40 commit b0d13f4

File tree

8 files changed

+753
-567
lines changed

8 files changed

+753
-567
lines changed

solution/0400-0499/0417.Pacific Atlantic Water Flow/README.md

Lines changed: 253 additions & 186 deletions
Large diffs are not rendered by default.

solution/0400-0499/0417.Pacific Atlantic Water Flow/README_EN.md

Lines changed: 260 additions & 193 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,48 @@
1-
typedef pair<int, int> pii;
2-
31
class Solution {
42
public:
5-
vector<vector<int>> heights;
6-
int m;
7-
int n;
8-
93
vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
10-
m = heights.size();
11-
n = heights[0].size();
12-
this->heights = heights;
13-
queue<pii> q1;
14-
queue<pii> q2;
15-
unordered_set<int> vis1;
16-
unordered_set<int> vis2;
4+
int m = heights.size(), n = heights[0].size();
5+
vector<vector<bool>> vis1(m, vector<bool>(n, false)), vis2(m, vector<bool>(n, false));
6+
queue<pair<int, int>> q1, q2;
7+
vector<int> dirs = {-1, 0, 1, 0, -1};
8+
179
for (int i = 0; i < m; ++i) {
18-
for (int j = 0; j < n; ++j) {
19-
if (i == 0 || j == 0) {
20-
vis1.insert(i * n + j);
21-
q1.emplace(i, j);
22-
}
23-
if (i == m - 1 || j == n - 1) {
24-
vis2.insert(i * n + j);
25-
q2.emplace(i, j);
26-
}
27-
}
10+
q1.emplace(i, 0);
11+
vis1[i][0] = true;
12+
q2.emplace(i, n - 1);
13+
vis2[i][n - 1] = true;
2814
}
29-
bfs(q1, vis1);
30-
bfs(q2, vis2);
31-
vector<vector<int>> ans;
32-
for (int i = 0; i < m; ++i) {
33-
for (int j = 0; j < n; ++j) {
34-
int x = i * n + j;
35-
if (vis1.count(x) && vis2.count(x)) {
36-
ans.push_back({i, j});
37-
}
38-
}
15+
for (int j = 0; j < n; ++j) {
16+
q1.emplace(0, j);
17+
vis1[0][j] = true;
18+
q2.emplace(m - 1, j);
19+
vis2[m - 1][j] = true;
3920
}
40-
return ans;
41-
}
4221

43-
void bfs(queue<pii>& q, unordered_set<int>& vis) {
44-
vector<int> dirs = {-1, 0, 1, 0, -1};
45-
while (!q.empty()) {
46-
for (int k = q.size(); k > 0; --k) {
47-
auto p = q.front();
22+
auto bfs = [&](queue<pair<int, int>>& q, vector<vector<bool>>& vis) {
23+
while (!q.empty()) {
24+
auto [x, y] = q.front();
4825
q.pop();
49-
for (int i = 0; i < 4; ++i) {
50-
int x = p.first + dirs[i];
51-
int y = p.second + dirs[i + 1];
52-
if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second]) {
53-
vis.insert(x * n + y);
54-
q.emplace(x, y);
26+
for (int k = 0; k < 4; ++k) {
27+
int nx = x + dirs[k], ny = y + dirs[k + 1];
28+
if (nx >= 0 && nx < m && ny >= 0 && ny < n
29+
&& !vis[nx][ny]
30+
&& heights[nx][ny] >= heights[x][y]) {
31+
vis[nx][ny] = true;
32+
q.emplace(nx, ny);
5533
}
5634
}
5735
}
58-
}
36+
};
37+
38+
bfs(q1, vis1);
39+
bfs(q2, vis2);
40+
41+
vector<vector<int>> ans;
42+
for (int i = 0; i < m; ++i)
43+
for (int j = 0; j < n; ++j)
44+
if (vis1[i][j] && vis2[i][j])
45+
ans.push_back({i, j});
46+
return ans;
5947
}
60-
};
48+
};
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
func pacificAtlantic(heights [][]int) [][]int {
22
m, n := len(heights), len(heights[0])
3-
vis1 := make(map[int]bool)
4-
vis2 := make(map[int]bool)
5-
var q1 [][]int
6-
var q2 [][]int
3+
vis1 := make([][]bool, m)
4+
vis2 := make([][]bool, m)
5+
for i := range vis1 {
6+
vis1[i] = make([]bool, n)
7+
vis2[i] = make([]bool, n)
8+
}
9+
q1, q2 := [][2]int{}, [][2]int{}
10+
dirs := [5]int{-1, 0, 1, 0, -1}
11+
712
for i := 0; i < m; i++ {
8-
for j := 0; j < n; j++ {
9-
if i == 0 || j == 0 {
10-
vis1[i*n+j] = true
11-
q1 = append(q1, []int{i, j})
12-
}
13-
if i == m-1 || j == n-1 {
14-
vis2[i*n+j] = true
15-
q2 = append(q2, []int{i, j})
16-
}
17-
}
13+
q1 = append(q1, [2]int{i, 0})
14+
vis1[i][0] = true
15+
q2 = append(q2, [2]int{i, n - 1})
16+
vis2[i][n-1] = true
17+
}
18+
for j := 0; j < n; j++ {
19+
q1 = append(q1, [2]int{0, j})
20+
vis1[0][j] = true
21+
q2 = append(q2, [2]int{m - 1, j})
22+
vis2[m-1][j] = true
1823
}
19-
dirs := []int{-1, 0, 1, 0, -1}
20-
bfs := func(q [][]int, vis map[int]bool) {
24+
25+
bfs := func(q [][2]int, vis [][]bool) {
2126
for len(q) > 0 {
22-
for k := len(q); k > 0; k-- {
23-
p := q[0]
24-
q = q[1:]
25-
for i := 0; i < 4; i++ {
26-
x, y := p[0]+dirs[i], p[1]+dirs[i+1]
27-
if x >= 0 && x < m && y >= 0 && y < n && !vis[x*n+y] && heights[x][y] >= heights[p[0]][p[1]] {
28-
vis[x*n+y] = true
29-
q = append(q, []int{x, y})
30-
}
27+
x, y := q[0][0], q[0][1]
28+
q = q[1:]
29+
for k := 0; k < 4; k++ {
30+
nx, ny := x+dirs[k], y+dirs[k+1]
31+
if nx >= 0 && nx < m && ny >= 0 && ny < n &&
32+
!vis[nx][ny] && heights[nx][ny] >= heights[x][y] {
33+
vis[nx][ny] = true
34+
q = append(q, [2]int{nx, ny})
3135
}
3236
}
3337
}
3438
}
39+
3540
bfs(q1, vis1)
3641
bfs(q2, vis2)
42+
3743
var ans [][]int
3844
for i := 0; i < m; i++ {
3945
for j := 0; j < n; j++ {
40-
x := i*n + j
41-
if vis1[x] && vis2[x] {
46+
if vis1[i][j] && vis2[i][j] {
4247
ans = append(ans, []int{i, j})
4348
}
4449
}
4550
}
4651
return ans
47-
}
52+
}
Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,51 @@
11
class Solution {
2-
private int[][] heights;
3-
private int m;
4-
private int n;
5-
62
public List<List<Integer>> pacificAtlantic(int[][] heights) {
7-
m = heights.length;
8-
n = heights[0].length;
9-
this.heights = heights;
10-
Deque<int[]> q1 = new LinkedList<>();
11-
Deque<int[]> q2 = new LinkedList<>();
12-
Set<Integer> vis1 = new HashSet<>();
13-
Set<Integer> vis2 = new HashSet<>();
3+
int m = heights.length, n = heights[0].length;
4+
boolean[][] vis1 = new boolean[m][n];
5+
boolean[][] vis2 = new boolean[m][n];
6+
Deque<int[]> q1 = new ArrayDeque<>();
7+
Deque<int[]> q2 = new ArrayDeque<>();
8+
int[] dirs = {-1, 0, 1, 0, -1};
9+
1410
for (int i = 0; i < m; ++i) {
15-
for (int j = 0; j < n; ++j) {
16-
if (i == 0 || j == 0) {
17-
vis1.add(i * n + j);
18-
q1.offer(new int[] {i, j});
19-
}
20-
if (i == m - 1 || j == n - 1) {
21-
vis2.add(i * n + j);
22-
q2.offer(new int[] {i, j});
11+
q1.offer(new int[] {i, 0});
12+
vis1[i][0] = true;
13+
q2.offer(new int[] {i, n - 1});
14+
vis2[i][n - 1] = true;
15+
}
16+
for (int j = 0; j < n; ++j) {
17+
q1.offer(new int[] {0, j});
18+
vis1[0][j] = true;
19+
q2.offer(new int[] {m - 1, j});
20+
vis2[m - 1][j] = true;
21+
}
22+
23+
BiConsumer<Deque<int[]>, boolean[][]> bfs = (q, vis) -> {
24+
while (!q.isEmpty()) {
25+
var cell = q.poll();
26+
int x = cell[0], y = cell[1];
27+
for (int k = 0; k < 4; ++k) {
28+
int nx = x + dirs[k], ny = y + dirs[k + 1];
29+
if (nx >= 0 && nx < m && ny >= 0 && ny < n && !vis[nx][ny]
30+
&& heights[nx][ny] >= heights[x][y]) {
31+
vis[nx][ny] = true;
32+
q.offer(new int[] {nx, ny});
33+
}
2334
}
2435
}
25-
}
26-
bfs(q1, vis1);
27-
bfs(q2, vis2);
36+
};
37+
38+
bfs.accept(q1, vis1);
39+
bfs.accept(q2, vis2);
40+
2841
List<List<Integer>> ans = new ArrayList<>();
2942
for (int i = 0; i < m; ++i) {
3043
for (int j = 0; j < n; ++j) {
31-
int x = i * n + j;
32-
if (vis1.contains(x) && vis2.contains(x)) {
33-
ans.add(Arrays.asList(i, j));
44+
if (vis1[i][j] && vis2[i][j]) {
45+
ans.add(List.of(i, j));
3446
}
3547
}
3648
}
3749
return ans;
3850
}
39-
40-
private void bfs(Deque<int[]> q, Set<Integer> vis) {
41-
int[] dirs = {-1, 0, 1, 0, -1};
42-
while (!q.isEmpty()) {
43-
for (int k = q.size(); k > 0; --k) {
44-
int[] p = q.poll();
45-
for (int i = 0; i < 4; ++i) {
46-
int x = p[0] + dirs[i];
47-
int y = p[1] + dirs[i + 1];
48-
if (x >= 0 && x < m && y >= 0 && y < n && !vis.contains(x * n + y)
49-
&& heights[x][y] >= heights[p[0]][p[1]]) {
50-
vis.add(x * n + y);
51-
q.offer(new int[] {x, y});
52-
}
53-
}
54-
}
55-
}
56-
}
57-
}
51+
}
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
class Solution:
22
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
3-
def bfs(q, vis):
3+
def bfs(q: Deque[Tuple[int, int]], vis: List[List[bool]]) -> None:
44
while q:
5-
for _ in range(len(q)):
6-
i, j = q.popleft()
7-
for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
8-
x, y = i + a, j + b
9-
if (
10-
0 <= x < m
11-
and 0 <= y < n
12-
and (x, y) not in vis
13-
and heights[x][y] >= heights[i][j]
14-
):
15-
vis.add((x, y))
16-
q.append((x, y))
5+
x, y = q.popleft()
6+
for dx, dy in pairwise(dirs):
7+
nx, ny = x + dx, y + dy
8+
if (
9+
0 <= nx < m
10+
and 0 <= ny < n
11+
and not vis[nx][ny]
12+
and heights[nx][ny] >= heights[x][y]
13+
):
14+
vis[nx][ny] = True
15+
q.append((nx, ny))
1716

1817
m, n = len(heights), len(heights[0])
19-
vis1, vis2 = set(), set()
20-
q1 = deque()
21-
q2 = deque()
18+
vis1 = [[False] * n for _ in range(m)]
19+
vis2 = [[False] * n for _ in range(m)]
20+
q1: Deque[Tuple[int, int]] = deque()
21+
q2: Deque[Tuple[int, int]] = deque()
22+
dirs = (-1, 0, 1, 0, -1)
23+
2224
for i in range(m):
23-
for j in range(n):
24-
if i == 0 or j == 0:
25-
vis1.add((i, j))
26-
q1.append((i, j))
27-
if i == m - 1 or j == n - 1:
28-
vis2.add((i, j))
29-
q2.append((i, j))
25+
q1.append((i, 0))
26+
vis1[i][0] = True
27+
q2.append((i, n - 1))
28+
vis2[i][n - 1] = True
29+
30+
for j in range(n):
31+
q1.append((0, j))
32+
vis1[0][j] = True
33+
q2.append((m - 1, j))
34+
vis2[m - 1][j] = True
35+
3036
bfs(q1, vis1)
3137
bfs(q2, vis2)
32-
return [
33-
(i, j)
34-
for i in range(m)
35-
for j in range(n)
36-
if (i, j) in vis1 and (i, j) in vis2
37-
]
38+
39+
return [(i, j) for i in range(m) for j in range(n) if vis1[i][j] and vis2[i][j]]

0 commit comments

Comments
 (0)