|
| 1 | +/** |
| 2 | + * @param {number[][]} heights |
| 3 | + * @return {number[][]} |
| 4 | + */ |
| 5 | + |
| 6 | +// ✅ Time Complexity: O(m * n) |
| 7 | +// ✅ Space Complexity: O(m * n) |
| 8 | + |
| 9 | +var pacificAtlantic = function (heights) { |
| 10 | + const m = heights.length; |
| 11 | + const n = heights[0].length; |
| 12 | + |
| 13 | + // ❌ Array(m).fill(Array(n).fill(false)) → Incorrect (all rows share the same array) |
| 14 | + const pacific = Array.from({ length: m }, () => Array(n).fill(false)); |
| 15 | + const atlantic = Array.from({ length: m }, () => Array(n).fill(false)); |
| 16 | + |
| 17 | + // four possible movement directions: down, up, right, left |
| 18 | + const directions = [ |
| 19 | + [1, 0], // ⬇️ |
| 20 | + [-1, 0], // ⬆️ |
| 21 | + [0, 1], // ➡️ |
| 22 | + [0, -1], // ⬅️ |
| 23 | + ]; |
| 24 | + |
| 25 | + /** |
| 26 | + * Depth-First Search (DFS) to mark reachable cells |
| 27 | + * @param {number} row - current row index |
| 28 | + * @param {number} col - current column index |
| 29 | + * @param {boolean[][]} visited - visited cells |
| 30 | + * @param {number} prevHeight - previously visited cell's height |
| 31 | + */ |
| 32 | + |
| 33 | + const dfs = (row, col, visited, prevHeight) => { |
| 34 | + // No search needed: |
| 35 | + // 1) Out of bounds |
| 36 | + // 2) already visited |
| 37 | + // 3) current height < previous height |
| 38 | + if ( |
| 39 | + row < 0 || |
| 40 | + row >= m || |
| 41 | + col < 0 || |
| 42 | + col >= n || |
| 43 | + visited[row][col] || |
| 44 | + heights[row][col] < prevHeight |
| 45 | + ) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // mark current cell as visited |
| 50 | + visited[row][col] = true; |
| 51 | + |
| 52 | + // visit all four possible directions |
| 53 | + for (const [dr, dc] of directions) { |
| 54 | + dfs(row + dr, col + dc, visited, heights[row][col]); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + // start dfs from each border |
| 59 | + for (let i = 0; i < m; i++) { |
| 60 | + dfs(i, 0, pacific, heights[i][0]); // left border(Pacific ocean) |
| 61 | + dfs(i, n - 1, atlantic, heights[i][n - 1]); // right border(Atlantic ocean) |
| 62 | + } |
| 63 | + |
| 64 | + for (let j = 0; j < n; j++) { |
| 65 | + dfs(0, j, pacific, heights[0][j]); // top border(Pacific ocean) |
| 66 | + dfs(m - 1, j, atlantic, heights[m - 1][j]); // bottom border(Atlantic ocean) |
| 67 | + } |
| 68 | + |
| 69 | + let result = []; |
| 70 | + |
| 71 | + for (let r = 0; r < m; r++) { |
| 72 | + for (let c = 0; c < n; c++) { |
| 73 | + if (pacific[r][c] && atlantic[r][c]) { |
| 74 | + result.push([r, c]); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return result; |
| 79 | +}; |
| 80 | + |
| 81 | +// Example test |
| 82 | +const heights = [ |
| 83 | + [1, 2, 2, 3, 5], |
| 84 | + [3, 2, 3, 4, 4], |
| 85 | + [2, 4, 5, 3, 1], |
| 86 | + [6, 7, 1, 4, 5], |
| 87 | + [5, 1, 1, 2, 4], |
| 88 | +]; |
| 89 | + |
| 90 | +const expected = [ |
| 91 | + [0, 4], |
| 92 | + [1, 3], |
| 93 | + [1, 4], |
| 94 | + [2, 2], |
| 95 | + [3, 0], |
| 96 | + [3, 1], |
| 97 | + [4, 0], |
| 98 | +]; |
| 99 | + |
| 100 | +const output = pacificAtlantic(heights); |
| 101 | + |
| 102 | +if (JSON.stringify(output.sort()) === JSON.stringify(expected.sort())) { |
| 103 | + console.log("✅ Accepted\n"); |
| 104 | +} else { |
| 105 | + console.log("❌ Not Accepted\n"); |
| 106 | +} |
| 107 | + |
| 108 | +console.log("Input:", heights, "\n"); |
| 109 | +console.log("Output:", output, "\n"); |
| 110 | +console.log("Expected:", expected); |
| 111 | + |
0 commit comments