|
| 1 | +// 개인적으로 행렬에서 전파되는 방식은 BFS를 이용한 구현 방식을 선호한다. |
| 2 | +// 최적의 실행시간이 나오지 않는 것 같아서 DFS로 선회 |
| 3 | +class Solution { |
| 4 | + private int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; |
| 5 | + private int m, n; |
| 6 | + |
| 7 | + public List<List<Integer>> pacificAtlantic(int[][] heights) { |
| 8 | + m = heights.length; |
| 9 | + n = heights[0].length; |
| 10 | + |
| 11 | + boolean[][] pacific = new boolean[m][n]; |
| 12 | + boolean[][] atlantic = new boolean[m][n]; |
| 13 | + List<List<Integer>> result = new ArrayList<>(); |
| 14 | + |
| 15 | + |
| 16 | + for (int i = 0; i < m; i++) { |
| 17 | + dfs(heights, pacific, i, 0); |
| 18 | + dfs(heights, atlantic, i, n - 1); |
| 19 | + } |
| 20 | + for (int j = 0; j < n; j++) { |
| 21 | + dfs(heights, pacific, 0, j); |
| 22 | + dfs(heights, atlantic, m - 1, j); |
| 23 | + } |
| 24 | + |
| 25 | + for (int i = 0; i < m; i++) { |
| 26 | + for (int j = 0; j < n; j++) { |
| 27 | + if (pacific[i][j] && atlantic[i][j]) { |
| 28 | + result.add(Arrays.asList(i, j)); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return result; |
| 34 | + } |
| 35 | + |
| 36 | + private void dfs(int[][] heights, boolean[][] visited, int x, int y) { |
| 37 | + visited[x][y] = true; |
| 38 | + for (int[] dir : directions) { |
| 39 | + int newX = x + dir[0], newY = y + dir[1]; |
| 40 | + if (newX >= 0 && newX < m && newY >= 0 && newY < n && !visited[newX][newY] && heights[newX][newY] >= heights[x][y]) { |
| 41 | + dfs(heights, visited, newX, newY); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class Solution { |
| 48 | + private int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; |
| 49 | + private int m, n; |
| 50 | + |
| 51 | + public List<List<Integer>> pacificAtlantic(int[][] heights) { |
| 52 | + m = heights.length; |
| 53 | + n = heights[0].length; |
| 54 | + |
| 55 | + boolean[][] pacific = new boolean[m][n]; |
| 56 | + boolean[][] atlantic = new boolean[m][n]; |
| 57 | + List<List<Integer>> result = new ArrayList<>(); |
| 58 | + |
| 59 | + Queue<int[]> pacificQueue = new LinkedList<>(); |
| 60 | + Queue<int[]> atlanticQueue = new LinkedList<>(); |
| 61 | + |
| 62 | + for (int i = 0; i < m; i++) { |
| 63 | + pacificQueue.offer(new int[]{i, 0}); |
| 64 | + atlanticQueue.offer(new int[]{i, n - 1}); |
| 65 | + pacific[i][0] = true; |
| 66 | + atlantic[i][n - 1] = true; |
| 67 | + } |
| 68 | + for (int j = 0; j < n; j++) { |
| 69 | + pacificQueue.offer(new int[]{0, j}); |
| 70 | + atlanticQueue.offer(new int[]{m - 1, j}); |
| 71 | + pacific[0][j] = true; |
| 72 | + atlantic[m - 1][j] = true; |
| 73 | + } |
| 74 | + |
| 75 | + bfs(heights, pacificQueue, pacific); |
| 76 | + bfs(heights, atlanticQueue, atlantic); |
| 77 | + |
| 78 | + for (int i = 0; i < m; i++) { |
| 79 | + for (int j = 0; j < n; j++) { |
| 80 | + if (pacific[i][j] && atlantic[i][j]) { |
| 81 | + result.add(Arrays.asList(i, j)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + private void bfs(int[][] heights, Queue<int[]> queue, boolean[][] visited) { |
| 90 | + while (!queue.isEmpty()) { |
| 91 | + int[] cell = queue.poll(); |
| 92 | + int x = cell[0], y = cell[1]; |
| 93 | + |
| 94 | + for (int[] dir : directions) { |
| 95 | + int newX = x + dir[0], newY = y + dir[1]; |
| 96 | + if (newX >= 0 && newX < m && newY >= 0 && newY < n && !visited[newX][newY] && heights[newX][newY] >= heights[x][y]) { |
| 97 | + queue.offer(new int[]{newX, newY}); |
| 98 | + visited[newX][newY] = true; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments