|
| 1 | +class Solution { |
| 2 | + // O(m*n) time / O(m*n) space |
| 3 | + func pacificAtlanticBFS(_ heights: [[Int]]) -> [[Int]] { |
| 4 | + var result = [[Int]]() |
| 5 | + guard let first = heights.first else { |
| 6 | + return result |
| 7 | + } |
| 8 | + let m = heights.count |
| 9 | + let n = first.count |
| 10 | + |
| 11 | + var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m) |
| 12 | + var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m) |
| 13 | + let directions = [(1,0), (-1,0), (0,1), (0,-1)] |
| 14 | + |
| 15 | + func bfs(queue: [(Int, Int)], goes: inout [[Bool]]) { |
| 16 | + var queue = queue |
| 17 | + var head = 0 |
| 18 | + for (i, j) in queue { |
| 19 | + goes[i][j] = true |
| 20 | + } |
| 21 | + |
| 22 | + while head < queue.count { |
| 23 | + let (i, j) = queue[head] |
| 24 | + head += 1 |
| 25 | + |
| 26 | + for (di, dj) in directions { |
| 27 | + let ni = i + di |
| 28 | + let nj = j + dj |
| 29 | + if ni >= 0 |
| 30 | + && ni < m |
| 31 | + && nj >= 0 |
| 32 | + && nj < n |
| 33 | + && !goes[ni][nj] |
| 34 | + && heights[ni][nj] >= heights[i][j] { |
| 35 | + goes[ni][nj] = true |
| 36 | + queue.append((ni, nj)) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + var pacificStarts = [(Int, Int)]() |
| 43 | + var atlanticStarts = [(Int, Int)]() |
| 44 | + for i in 0..<m { |
| 45 | + pacificStarts.append((i, 0)) |
| 46 | + atlanticStarts.append((i, n-1)) |
| 47 | + } |
| 48 | + |
| 49 | + for j in 0..<n { |
| 50 | + pacificStarts.append((0, j)) |
| 51 | + atlanticStarts.append((m-1, j)) |
| 52 | + } |
| 53 | + |
| 54 | + bfs(queue: pacificStarts, goes: &goesPacific) |
| 55 | + bfs(queue: atlanticStarts, goes: &goesAtlantic) |
| 56 | + |
| 57 | + for i in 0..<m { |
| 58 | + for j in 0..<n { |
| 59 | + if goesPacific[i][j] && goesAtlantic[i][j] { |
| 60 | + result.append([i, j]) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return result |
| 65 | + } |
| 66 | + |
| 67 | + // O(m*n) time / O(m*n) space |
| 68 | + func pacificAtlanticDFS(_ heights: [[Int]]) -> [[Int]] { |
| 69 | + var result = [[Int]]() |
| 70 | + guard let first = heights.first else { |
| 71 | + return result |
| 72 | + } |
| 73 | + let m = heights.count |
| 74 | + let n = first.count |
| 75 | + |
| 76 | + var goesPacific = Array(repeating: Array(repeating: false, count: n), count: m) |
| 77 | + var goesAtlantic = Array(repeating: Array(repeating: false, count: n), count: m) |
| 78 | + let directions = [(1,0), (-1,0), (0,1), (0,-1)] |
| 79 | + |
| 80 | + func dfs(i: Int, j: Int, goes: inout [[Bool]]) { |
| 81 | + if goes[i][j] { |
| 82 | + return |
| 83 | + } |
| 84 | + goes[i][j] = true |
| 85 | + |
| 86 | + for (di, dj) in directions { |
| 87 | + let ni = i + di |
| 88 | + let nj = j + dj |
| 89 | + if ni >= 0 |
| 90 | + && ni < m |
| 91 | + && nj >= 0 |
| 92 | + && nj < n |
| 93 | + && heights[ni][nj] >= heights[i][j] { |
| 94 | + dfs(i: ni, j: nj, goes: &goes) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for i in 0..<m { |
| 100 | + dfs(i: i, j: 0, goes: &goesPacific) |
| 101 | + dfs(i: i, j: n-1, goes: &goesAtlantic) |
| 102 | + } |
| 103 | + |
| 104 | + for j in 0..<n { |
| 105 | + dfs(i: 0, j: j, goes: &goesPacific ) |
| 106 | + dfs(i: m-1, j: j, goes: &goesAtlantic ) |
| 107 | + } |
| 108 | + |
| 109 | + for i in 0..<m { |
| 110 | + for j in 0..<n { |
| 111 | + if goesPacific[i][j] && goesAtlantic[i][j] { |
| 112 | + result.append([i, j]) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return result |
| 117 | + } |
| 118 | +} |
0 commit comments