Multi-source BFS, which I knew going in, but I fumbled the -1 case for a solid two minutes.
Model the grid as a graph and use multi-source BFS starting from all initially infected cells simultaneously. Track the time each healthy cell becomes infected, and after BFS, check if any healthy cells remain uninfected; if so, return -1, otherwise return the maximum time.
Pro tip: Clarify edge cases upfront (e.g., no healthy cells, no infected cells, unreachable healthy cells) and mention that BFS is optimal because infection spreads uniformly in time, similar to shortest path in an unweighted graph.
Restate the problem: infection spreads to 4-directional healthy neighbors each minute. Identify edge cases: no healthy cells (return 0), no infected cells but healthy cells exist (return -1), and healthy cells unreachable from any infected cell (return -1).
Recognize this as a multi-source shortest path problem on an unweighted grid. BFS is ideal because it explores level by level, naturally tracking the minute each cell gets infected.
Initialize a queue with all infected cells and set their time to 0. While the queue is not empty, pop a cell and for each healthy neighbor, mark it infected, set its time to current time + 1, and enqueue it. Keep track of the maximum time.
After BFS, scan the grid to see if any healthy cells remain. If yes, return -1; otherwise, return the maximum time recorded.
Time complexity is O(m*n) since each cell is processed once. Space complexity is O(m*n) for the queue and time tracking. Mention that in-place modification of the grid can save space if allowed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.