๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Study Note/LeetCode

1162. As Far from Land as Possible

by Jellll_y 2023. 2. 10.

1162. As Far from Land as Possible

Medium


Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.

The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.

 

Example 1:

Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
Output: 2
Explanation: The cell (1, 1) is as far as possible from all the land with distance 2.

Example 2:

Input: grid = [[1,0,0],[0,0,0],[0,0,0]]
Output: 4
Explanation: The cell (2, 2) is as far as possible from all the land with distance 4.

 

Constraints:

  • n == grid.length
  • n == grid[i].length
  • 1 <= n <= 100
  • grid[i][j] is 0 or 1

 

๋ฌธ์ œ

grid[i] === 0 // ๋ฌผ

grid[i] === 1 // ๋•…

๋•…์—์„œ ๊ฐ€์žฅ ๋ฉ€๋ฆฌ ๋–จ์–ด์ง„ ๋ฌผ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ์ฐพ๋Š” ๋ฌธ์ œ

๋ฌธ์ œ ์ ‘๊ทผ๋ฒ•

๋™์‹œ์— ํƒ์ƒ‰ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— BFS ์‚ฌ์šฉํ•˜์—ฌ ํ’€์ด

๋ชจ๋“  ๋•…์—์„œ ๋™์‹œ์— ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ์ตœ๋Œ€ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š”๋‹ค.

manhattan distance ์‚ฌ์šฉ x

์˜์‚ฌ ์ฝ”๋“œ

  1. ๋ณ€์ˆ˜ queue์— ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  grid๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋•…์˜ ์ขŒํ‘œ์™€ ๊ฑฐ๋ฆฌ 0์„ ํ‘ธ์‹œํ•ด ์ค€๋‹ค.
  2. maxDistance๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  -1๋กœ ์ดˆ๊ธฐํ™”ํ•ด ์ค€๋‹ค.
  3. BFS ํƒ์ƒ‰
    1. maxDistance ์—…๋ฐ์ดํŠธ
    2. 4๊ฐ€์ง€ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ํ•˜๋ฉฐ grid๊ฐ€ ์œ ํšจํ•œ์ง€ ์ฒดํฌํ•œ๋‹ค
      1. ์ด๋™ํ•œ ์ขŒํ‘œ๊ฐ€ ์œ ํšจํ•˜๋‹ค๋ฉด grid์—์„œ ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋•…์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  queue์— ํ‘ธ์‹œํ•ด ์ค€๋‹ค.
  4. maxDistance๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์ด๋•Œ maxDistance๊ฐ€ 0์ด๋ผ๋ฉด ์œก์ง€ ๋˜๋Š” ๋ฌผ์ด ์—†๋‹ค๊ณ  ํŒ๋‹จํ•˜์—ฌ -1์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.
/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxDistance = function(grid) {
    const m = grid.length, n = grid[0].length
    const directions = [[-1,0],[0,1],[1,0],[0,-1]]
    
    // grid์—์„œ ๋ฐฉํ–ฅ์ด๋™ํ• ๋•Œ ์œ ํšจํ•œ์ง€ ์ฒดํฌ 
    const gridIsValid = (x,y) => {
        return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 0
    }
    
    let queue = [] 
    
    // ๋•…์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ queue์— push
    for(let row=0; row<m; row++){
        for(let col=0; col<n; col++){
            if(grid[row][col] === 1) {
                queue.push([row,col,0])
            }
        }
    }
    
    let maxDistance = -1
    
    while(queue.length){
        const nextQueue = []
        
        for(const [x,y,distance] of queue){
            maxDistance = Math.max(maxDistance, distance) 
            for(const [dx,dy] of directions){
                const nx = x + dx, ny = y + dy

                if(gridIsValid(nx,ny)){
                    grid[nx][ny] = 1
                    nextQueue.push([nx,ny,distance+1])
                }
            }
        }
        queue = nextQueue
    }
    
    return maxDistance === 0 ? -1 : maxDistance
};

'๐Ÿ“š Study Note > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

542. 01 Matrix  (0) 2023.02.14
2306. Naming a Company  (0) 2023.02.09
904. Fruit Into Baskets  (0) 2023.02.07
1091. Shortest Path in Binary Matrix  (0) 2023.02.06
2091. Removing Minimum and Maximum From Array  (0) 2023.02.01