<filename>994-rotting-oranges/994-rotting-oranges.py
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        # BFS instead of DFS! 
        # Need a multi source BFS, it's a must. 
        
        q = deque()
        
        time, fresh = 0, 0
        
        rows = len(grid)
        cols = len(grid[0])
        
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == 1:
                    fresh += 1
                if grid[r][c] == 2:
                    q.append([r, c])

        directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
        while q and fresh > 0:
            
            #len(q) = 3
            for i in range(len(q)):
                r, c = q.popleft()
                
                for dr, dc in directions:
                    row, col = dr + r, dc + c
                    
                    if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] != 1:
                        continue
                        
                    grid[row][col] = 2
                    q.append([row, col])
                    
                    fresh -= 1
            time += 1
            
        return time if fresh == 0 else -1
<filename>994-rotting-oranges/994-rotting-oranges.py
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        # BFS instead of DFS! 
        # Need a multi source BFS, it's a must. 
        
        q = deque()
        
        time, fresh = 0, 0
        
        rows = len(grid)
        cols = len(grid[0])
        
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == 1:
                    fresh += 1
                if grid[r][c] == 2:
                    q.append([r, c])

        directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
        while q and fresh > 0:
            
            #len(q) = 3
            for i in range(len(q)):
                r, c = q.popleft()
                
                for dr, dc in directions:
                    row, col = dr + r, dc + c
                    
                    if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] != 1:
                        continue
                        
                    grid[row][col] = 2
                    q.append([row, col])
                    
                    fresh -= 1
            time += 1
            
        return time if fresh == 0 else -1
# BFS instead of DFS! # Need a multi source BFS, it's a must. #len(q) = 3