Tensorum
YouTubeಕಲಿಯಲು ಪ್ರಾರಂಭಿಸಿ
LeetCode 994 · Multi-Source BFS

Rotting Oranges

LeetCode 994 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್‌ಥ್ರೂ

Multi-source BFS from rotten oranges; check none stay fresh.

ಮಧ್ಯಮArrayBreadth-First SearchMatrix
LeetCode ನಲ್ಲಿ ತೆರೆಯಿರಿ ↗

ನಾನು Rotting Oranges (LeetCode 994) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Multi-Source BFS ಪ್ಯಾಟರ್ನ್‌ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.

ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್‌ನಲ್ಲಿದೆ.

Intuition

Rot spreads one step per minute from every rotten orange at the same time — exactly a multi-source BFS. The time of the last orange to rot is the answer, unless some fresh orange is never reached.

Approach

  1. 1

    Queue all rotten oranges at time 0 and count fresh ones.

  2. 2

    BFS into fresh neighbours with time + 1.

  3. 3

    Track the largest time.

  4. 4

    If not every fresh orange was reached, −1.

ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ

Multi-source BFSPythonO(m·n) time
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        #bfs - step by step, level by level
        #will start with rotten oranges, if next node is fresh it can be rotten in time+1 minute
        #calculate rotten and fresh oranges if at end both are same then return maximum time else -1
        q = deque([])
        visited = set()
        m = len(grid)
        n = len(grid[0])
        fresh_oranges = 0
        rotten_oranges = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j]==2:
                     q.append((i,j,0))
                     visited.add((i,j))
                if grid[i][j]==1:
                    fresh_oranges+=1
        max_time = 0
        di = [0,+1,0,-1]
        dj = [+1,0,-1,0]
        while q:
            i,j,time = q.popleft()
            max_time= max(max_time,time)
            for k in range(4):
                nrow = i+di[k]
                ncol = j+dj[k]
                if 0<=nrow<m and 0<=ncol<n and grid[nrow][ncol]==1:
                    if (nrow,ncol) not in visited:
                        rotten_oranges+=1
                        q.append((nrow,ncol,time+1))
                        visited.add((nrow,ncol))
        return max_time if fresh_oranges==rotten_oranges else -1
            
                    
ಸ್ಟೆಪ್-ಥ್ರೂ ಲೋಡ್ ಆಗುತ್ತಿದೆ…

Rotting Oranges — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು

Rotting Oranges ಹಿಂದಿನ intuition ಏನು?

Rot spreads one step per minute from every rotten orange at the same time — exactly a multi-source BFS. The time of the last orange to rot is the answer, unless some fresh orange is never reached.

Rotting Oranges (LeetCode 994) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?

1. Queue all rotten oranges at time 0 and count fresh ones. 2. BFS into fresh neighbours with time + 1. 3. Track the largest time. 4. If not every fresh orange was reached, −1.

Rotting Oranges ನ time ಮತ್ತು space complexity ಎಷ್ಟು?

Time: O(m · n). Space: O(m · n). (Accepted approach: Multi-source BFS.)

Rotting Oranges ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?

No fresh oranges at all → 0 minutes.