The immune cells are the real wrinkle here.
Model the grid as a graph and use multi-source BFS where all initially infected cells are sources. Each BFS layer represents one day, and you track the maximum distance to any healthy cell that gets infected; if some healthy cells remain unreachable, the infection stops after the last layer that infected at least one cell.
Pro tip: Clarify edge cases upfront: if there are no infected cells, return 0; if all healthy cells are blocked by immune cells, return 0; and if the grid is empty, return 0. Also, mention that you'll use a queue to process cells level by level to naturally count days.
Confirm the grid dimensions, the meaning of each cell type, and what should be returned if no spread occurs. Discuss edge cases like empty grid, no infected cells, or all cells immune.
Explain that multi-source BFS is ideal because it simulates simultaneous spread from multiple sources and naturally tracks the number of days as BFS levels.
Enqueue all initially infected cells with distance 0. Initialize a variable to track the maximum days and a count of remaining healthy cells.
For each level, process all cells currently in the queue, infecting all 8 neighboring healthy cells, marking them infected, and enqueueing them with distance+1. Increment the day counter after each level if any new infections occurred.
After BFS, if all healthy cells are infected, return the maximum distance; otherwise, return the maximum distance achieved (since infection stops). State time and space complexity: O(R*C) time and O(R*C) space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.