classSolution { int m, n; int[][] grid; int[][] dirs = newint[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public List<List<Integer>> pacificAtlantic(int[][] heights) { grid = heights; m = grid.length; n = grid[0].length; Deque<int[]> q1 = newArrayDeque<>(), q2 = newArrayDeque<>(); boolean[][] res1 = newboolean[m][n], res2 = newboolean[m][n]; for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { if (i == 0 || j == 0) { res1[i][j] = true; q1.offer(newint[]{i, j}); } if (i == m - 1 || j == n - 1) { res2[i][j] = true; q2.offer(newint[]{i, j}); } } } bfs(q1, res1); bfs(q2, res2); List<List<Integer>> ans = newArrayList<>(); for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { if (res1[i][j] && res2[i][j]) { List<Integer> list = newArrayList<>(); list.add(i); list.add(j); ans.add(list); } } } return ans; }
privatevoidbfs(Deque<int[]> q, boolean[][] res) { while (!q.isEmpty()) { int[] poll = q.poll(); intx= poll[0], y = poll[1], t = grid[x][y]; for (int[] dir : dirs) { intnx= x + dir[0], ny = y + dir[1]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue; if (res[nx][ny] || grid[nx][ny] < t) continue; q.offer(newint[]{nx, ny}); res[nx][ny] = true; } } } }
classSolution { int m, n; int[][] grid; int[][] dirs = newint[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public List<List<Integer>> pacificAtlantic(int[][] heights) { grid = heights; m = grid.length; n = grid[0].length; boolean[][] res1 = newboolean[m][n], res2 = newboolean[m][n]; for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { if (i == 0 || j == 0) { if (!res1[i][j]) dfs(i, j, res1); } if (i == m - 1 || j == n - 1) { if (!res2[i][j]) dfs(i, j, res2); } } } List<List<Integer>> ans = newArrayList<>(); for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { if (res1[i][j] && res2[i][j]) { List<Integer> list = newArrayList<>(); list.add(i); list.add(j); ans.add(list); } } } return ans; }
privatevoiddfs(int x, int y, boolean[][] res) { res[x][y] = true; for (int[] dir : dirs) { intnx= x + dir[0], ny = y + dir[1]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue; if (res[nx][ny] || grid[nx][ny] < grid[x][y]) continue; dfs(nx, ny, res); } } }
privateintfind(int[] p, int x) { if (p[x] != x) p[x] = find(p, p[x]); return p[x]; }
privatevoidunion(int[] p, int a, int b) { p[find(p, a)] = p[find(p, b)]; }
privatebooleanquery(int[] p, int a, int b) { return find(p, a) == find(p, b); }
privateintgetIndex(int i, int j) { return i * n + j; }
public List<List<Integer>> pacificAtlantic(int[][] heights) { grid = heights; m = grid.length; n = grid[0].length; total = m * n; S = total + 1; T = total + 2; for (inti=0; i <= T; ++i) p1[i] = p2[i] = i; for (inti=0; i < m; ++i) { for (intj=0; j < n; ++j) { intindex= getIndex(i, j); if (i == 0 || j == 0) { if (!query(p1, S, index)) dfs(p1, S, i, j); } if (i == m - 1 || j == n - 1) { if (!query(p2, T, index)) dfs(p2, T, i, j); } } } List<List<Integer>> ans = newArrayList<>(); for (inti=0; i < m; i++) { for (intj=0; j < n; j++) { intindex= getIndex(i, j); if (query(p1, S, index) && query(p2, T, index)) { List<Integer> list = newArrayList<>(); list.add(i); list.add(j); ans.add(list); } } } return ans; }
privatevoiddfs(int[] p, int o, int x, int y) { union(p, o, getIndex(x, y)); for (int[] dir : dirs) { intnx= x + dir[0], ny = y + dir[1]; if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue; if (query(p, o, getIndex(nx, ny)) || grid[nx][ny] < grid[x][y]) continue; dfs(p, o, nx, ny); } } }