| 3 | int[][] dir = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } }; |
| 4 | |
| 5 | public List<List<Integer>> pacificAtlantic(int[][] heights) { |
| 6 | List<List<Integer>> res = new ArrayList<>(); |
| 7 | |
| 8 | int rows = heights.length, cols = heights[0].length; |
| 9 | boolean[][] pacific = new boolean[rows][cols]; |
| 10 | boolean[][] atlantic = new boolean[rows][cols]; |
| 11 | |
| 12 | for (int i = 0; i < cols; i++) { |
| 13 | dfs(heights, 0, i, Integer.MIN_VALUE, pacific); |
| 14 | dfs(heights, rows - 1, i, Integer.MIN_VALUE, atlantic); |
| 15 | } |
| 16 | |
| 17 | for (int i = 0; i < rows; i++) { |
| 18 | dfs(heights, i, 0, Integer.MIN_VALUE, pacific); |
| 19 | dfs(heights, i, cols - 1, Integer.MIN_VALUE, atlantic); |
| 20 | } |
| 21 | |
| 22 | for (int i = 0; i < rows; i++) { |
| 23 | for (int j = 0; j < cols; j++) { |
| 24 | if (pacific[i][j] && atlantic[i][j]) { |
| 25 | res.add(List.of(i, j)); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | return res; |
| 30 | } |
| 31 | |
| 32 | private void dfs( |
| 33 | int[][] heights, |