|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Arrays; |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Queue; |
| 6 | + |
| 7 | +class Solution { |
| 8 | + |
| 9 | + private final int[] dy = { -1, 1, 0, 0 }; |
| 10 | + private final int[] dx = { 0, 0, -1, 1 }; |
| 11 | + private int M = 0, N = 0; |
| 12 | + |
| 13 | + /** |
| 14 | + * ํน์ ๋
ธ๋์์ ์์์์ ์ด์ ๋
ธ๋๋ฅผ ๊ฑฐ์ณ ๋ ๋ฐ๋ค๋ก ๊ฐ ์ ์๋ ๋
ธ๋ ๋ฆฌ์คํธ๋ฅผ ์ถ๋ ฅํ๊ธฐ |
| 15 | + * ํน์ ๋
ธ๋์์ ์ ์ฒด ํ์? => O(NxM) x O(NxM) ํ์ ํ์. |
| 16 | + * |
| 17 | + * ๊ทธ๋ผ ํํ์, ๋์์ ๊ฐ๊ฐ์์ ๋๋ฌํ ์ ์๋ ๋
ธ๋ ๋ฆฌ์คํธ ์ฐพ๊ธฐ(๋ณธ์ธ ์ด์ ๊ฐ์ ๊ฐ์ง ๋
ธ๋๋ก ํ์) |
| 18 | + * ๊ทธ๋ฆฌ๊ณ ๊ฒน์น๋ ์ง์ ์ฐพ๊ธฐ => ๋ต |
| 19 | + * 3xO(NxM) |
| 20 | + * |
| 21 | + * Runtime: 11 ms (Beats 28.23%) |
| 22 | + * Memory: 46.9 MB (Beats 98.39%) |
| 23 | + * Space Complexity: O(NxM) |
| 24 | + * - ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฌ๋ถ๋ฅผ ์ฒดํฌํ๊ธฐ ์ํ flowed O(NxM) |
| 25 | + * - ๋ฐฉ๋ฌธํ๋ ๋
ธ๋๋ฅผ ํ์ ์ฐพ๊ธฐ qu O(NxM) |
| 26 | + * > 2O(NxM) => O(NxM) |
| 27 | + * Time Complexity: O(NxM) |
| 28 | + * - ํํ์ ์ธ์ ๋
ธ๋ ํ์ O(NxM) |
| 29 | + * - ๋์์ ์ธ์ ๋
ธ๋ ํ์ O(NxM) |
| 30 | + * - ๋ ๋์ ์ธ์ ๋
ธ๋ ๊ฒ์ O(NxM) |
| 31 | + * > 3O(NxM) |
| 32 | + */ |
| 33 | + public List<List<Integer>> pacificAtlantic(int[][] heights) { |
| 34 | + M = heights.length; |
| 35 | + N = heights[0].length; |
| 36 | + int[][] flowed = new int[M][N]; // 1์ด๋ฉด ํํ์ ์ธ์ , 2๋ฉด ๋์์, 3์ด๋ฉด ๋ ๋ค, 0์ด๋ฉด ์ธ์ X |
| 37 | + |
| 38 | + // (0,0) ~ (0,N-1)์ 1๋ก ๋ฃ๊ณ bfs ๋๋ฆฌ๊ธฐ |
| 39 | + Queue<int[]> qu = new LinkedList<>(); |
| 40 | + for (int i = 0; i < N; i++) { |
| 41 | + int[] temp = { 0, i }; |
| 42 | + flowed[0][i] += 1; |
| 43 | + qu.add(temp); |
| 44 | + } |
| 45 | + // (1, 0) ~ (M-1, 0) |
| 46 | + for (int i = 1; i < M; i++) { |
| 47 | + int[] temp = { i, 0 }; |
| 48 | + flowed[i][0] += 1; |
| 49 | + qu.add(temp); |
| 50 | + } |
| 51 | + // ํํ์ ํ์ |
| 52 | + bfs(qu, flowed, heights, 1); |
| 53 | + |
| 54 | + // printMp(flowed); |
| 55 | + |
| 56 | + // System.out.println("------------"); |
| 57 | + |
| 58 | + qu = new LinkedList<>(); |
| 59 | + // (M-1, 0) ~ (M-1, N-1) |
| 60 | + for (int i = 0; i < N; i++) { |
| 61 | + int[] temp = { M - 1, i }; |
| 62 | + flowed[M - 1][i] += 2; |
| 63 | + qu.add(temp); |
| 64 | + } |
| 65 | + // (0, 0) ~ (M-2, N-1) |
| 66 | + for (int i = 0; i < M - 1; i++) { |
| 67 | + int[] temp = { i, N - 1 }; |
| 68 | + flowed[i][N - 1] += 2; |
| 69 | + qu.add(temp); |
| 70 | + } |
| 71 | + // ํํ์ ํ์ |
| 72 | + bfs(qu, flowed, heights, 2); |
| 73 | + List<List<Integer>> ans = new ArrayList<>(); |
| 74 | + |
| 75 | + for (int i = 0; i < M; i++) { |
| 76 | + for (int j = 0; j < N; j++) { |
| 77 | + if (flowed[i][j] == 3) { |
| 78 | + List<Integer> tmp = new ArrayList<>(Arrays.asList(i, j)); |
| 79 | + ans.add(tmp); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return ans; |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + private void bfs(Queue<int[]> qu, int[][] flowed, int[][] heights, int oceanTp) { |
| 89 | + while (!qu.isEmpty()) { |
| 90 | + int[] node = qu.poll(); |
| 91 | + int h = heights[node[0]][node[1]]; |
| 92 | + |
| 93 | + // ์ฌ๋ฐฉ์ ๋ณด๋ฉด์ ์ ์ ํ ๋
ธ๋๋ฅผ ํ์ ๋ฃ๊ธฐ |
| 94 | + for (int n = 0; n < 4; n++) { |
| 95 | + int ny = node[0] + dy[n]; |
| 96 | + int nx = node[1] + dx[n]; |
| 97 | + |
| 98 | + // ์ฌ ๋ฐ ์์น์ธ์ง ์ฒดํฌ |
| 99 | + if (ny < 0 || ny >= M || nx < 0 || nx >= N) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + // ๋ฐฉ๋ฌธํ๋ ์์น๋ฉด ๋์ด๊ฐ๊ธฐ |
| 103 | + if (flowed[ny][nx] >= oceanTp) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + // ๋ฐ๋ค์์ ์ฌ์ผ๋ก ๊ฐ ์ ์๋ ๋ฐฉํฅ์ธ์ง ์ฒดํฌ |
| 107 | + if (h > heights[ny][nx]) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + flowed[ny][nx] += oceanTp; |
| 111 | + int[] temp = { ny, nx }; |
| 112 | + qu.add(temp); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void printMp(int[][] mp) { |
| 118 | + int my = mp.length; |
| 119 | + int mx = mp[0].length; |
| 120 | + for (int y = 0; y < my; y++) { |
| 121 | + for (int x = 0; x < mx; x++) { |
| 122 | + System.out.print(mp[y][x] + " "); |
| 123 | + } |
| 124 | + System.out.print('\n'); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments