Swift💯BFS 🫧 Clean
BFS (accepted answer)
Implementation note: Using queue.removeFirst() is too slow, instead, keep track with read index.
**BFS (accepted answer)**
Implementation note: Using `queue.removeFirst()` is too slow, instead, keep track with read index.
```
class Solution {
  func highestPeak(_ grid: consuming [[Int]]) -> [[Int]] {
    let (rows, cols) = (grid.indices, grid[0].indices)
    var queue = product(cols,rows).compactMap {(x,y)->(Int,Int)? in
      grid[y][x] -= 1  //side-effect: water = 0, unvisited = -1  
      return grid[y][x] == 0 ? (x,y) : nil // only add water tiles
    }
    for i in 0 ..< rows.count * cols.count {
      let (x,y) = queue[i], height = grid[y][x]
      for (dx,dy) in [0,1,0,-1,0].adjacentPairs() {
        let (c,r) = (x+dx, y+dy)
        guard cols ~= c, rows ~= r, grid[r][c] == -1 else { continue }
        grid[r][c] = height+1
        queue.append((c,r))
      }
    }
    return grid
  }
}
```
['Swift']