Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func pacificAtlantic(_ matrix: [[Int]]) -> [[Int]] {
- guard matrix.count > 0 && matrix[0].count > 0 else { return [] }
- let n = matrix.count
- let m = matrix[0].count
- var dpleft = [[Bool]](repeating: [Bool](repeating : false, count : m), count: n)
- var dpright = [[Bool]](repeating: [Bool](repeating : false, count : m), count: n)
- var res = [[Int]]()
- for i in 0 ..< n {
- fillWater(matrix, &dpleft, i, 0)
- fillWater(matrix, &dpright, i, m - 1)
- }
- for j in 0 ..< m {
- fillWater(matrix, &dpleft, 0, j)
- fillWater(matrix, &dpright, n - 1, j)
- }
- for i in 0 ..< n {
- for j in 0 ..< m{
- if dpleft[i][j] && dpright[i][j]{
- res.append([i, j])
- }
- }
- }
- return res
- }
- let dx = [1, -1 ,0 ,0 ]
- let dy = [0 ,0 ,1 ,-1 ]
- func fillWater(_ m: [[Int]], _ check: inout [[Bool]], _ i:Int, _ j:Int){
- check[i][j] = true
- for k in 0 ..< 4 {
- let nx = dx[k] + i
- let ny = dy[k] + j
- if nx >= 0 && ny >= 0 && nx < m.count && ny < m[0].count && m[nx][ny] >= m[i][j] && !check[nx][ny] {
- fillWater(m, &check, nx, ny)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement