λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸƒ algorithm/leetcode

LeetCode 994 - Rotting Oranges (Medium)

by HandHand 2023. 4. 24.

 

πŸ’‘ 문제

LeetCode - 994번

 

🎯 풀이 κ³Όμ •

맀 λ‹¨μœ„μ‹œκ°„(minute) λ§ˆλ‹€ 썩은 μ˜€λ Œμ§€μ— μΈμ ‘ν•œ 4λ°©ν–₯에 μœ„μΉ˜ν•œ μ˜€λ Œμ§€κ°€ ν•¨κ»˜ μ©λŠ”λ‹€κ³  ν• λ•Œ

주어진 grid μ—μ„œ λͺ¨λ“  μ˜€λ Œμ§€κ°€ μ©κΈ°κΉŒμ§€ κ±Έλ¦¬λŠ” μ‹œκ°„μ„ κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

 

일반적인 BFS νƒμƒ‰μ—μ„œ μ•½κ°„μ˜ λ³€ν˜•μ΄ ν•„μš”ν•œ μœ ν˜•μž…λ‹ˆλ‹€.

썩은 μ˜€λ Œμ§€λ₯Ό queue 에 λ„£μ–΄μ£Όκ³  queue κ°€ λΉ„μ–΄μ§ˆλ•ŒκΉŒμ§€ λ°˜λ³΅ν•΄μ•Όμ§€

각각의 λ‹¨μœ„ μ‹œκ°„λ§ˆλ‹€ ν˜„μž¬ queue 에 λ‹΄κ²¨μžˆλŠ” μ˜€λ Œμ§€λ“€μ— λŒ€ν•΄ BFS λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

보톡 BFS λ₯Ό ν•  λ•Œ 쀑볡 방문을 ν”Όν•˜κΈ° μœ„ν•΄ visit 같은 자료ꡬ쑰λ₯Ό λ§Œλ“€μ–΄ μ΄μš©ν•˜λŠ”λ°,

μ—¬κΈ°μ„œλŠ” μ˜€λ Œμ§€κ°€ 썩은 μƒνƒœμΌ λ•Œλ₯Ό 이미 λ°©λ¬Έν•œ μœ„μΉ˜λ‘œ νŒλ‹¨μ΄ κ°€λŠ₯ν•˜λ―€λ‘œ

λ°©λ¬Έμ—¬λΆ€λ₯Ό μœ„ν•œ λ³„λ„μ˜ μžλ£Œκ΅¬μ‘°λŠ” λ§Œλ“€μ–΄μ£Όμ§€ μ•Šκ³  μƒνƒœκ°’μœΌλ‘œ νŒλ‹¨ν•˜λ„λ‘ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

πŸ‘¨‍πŸ’» μ½”λ“œ

/**
 * @param {number[][]} grid
 * @return {number}
 */
var orangesRotting = function(grid) {
    const [ROW, COL] = [grid.length, grid[0].length]
    const dr = [1, -1, 0, 0]
    const dc = [0, 0, 1, -1]
    const queue = []

    function preparation() {
        for (let row = 0; row < ROW; row += 1) {
            for (let col = 0; col < COL; col += 1) {
                const isRotten = grid[row][col] === 2
                if (isRotten) {
                    queue.push([row, col])
                }
            }
        }
    }

    function outOfRange(row, col) {
        return row < 0 || row >= ROW || col < 0 || col >= COL
    }

    function rottening() {
        let minute = -1

        while (queue.length > 0) {
            let iterate = queue.length

            while (iterate > 0) {
                const [hr, hc] = queue.shift()

                for (let dir = 0; dir < 4; dir += 1) {
                    const [nr, nc] = [dr[dir] + hr, dc[dir] + hc]
                    if (outOfRange(nr, nc)) {
                        continue
                    }

                    const isEmpty = grid[nr][nc] === 0
                    const isRotten = grid[nr][nc] === 2

                    if (isEmpty || isRotten) {
                        continue
                    }

                    grid[nr][nc] = 2 // now, adjacent orange is rottening
                    queue.push([nr, nc])
                }

                iterate -= 1
            }

            minute += 1
        }

        return minute
    }  

    preparation()
    const minute = rottening()

    for (let row = 0; row < ROW; row += 1) {
        for (let col = 0; col < COL; col += 1) {
            const isFresh = grid[row][col] === 1
            if (isFresh) {
                return -1
            }
        }
    }

    return Math.max(minute, 0)
};
λ°˜μ‘ν˜•

'πŸƒ algorithm > leetcode' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

LeetCode 15 - 3Sum (Medium)  (0) 2023.04.24
LeetCode 13 - Roman to Integer (Easy)  (0) 2023.04.24
LeetCode 24 - Swap Nodes in Pairs (Medium)  (0) 2023.02.20
LeetCode 35 - Search Insert Position (Easy)  (0) 2023.02.20
LeetCode 169 - Majority Element (Easy)  (2) 2023.02.05

πŸ’¬ λŒ“κΈ€