Multi-source BFS, pretty standard once you recognize it.
Model the grid as a graph and use multi-source BFS starting from all initially infected cells simultaneously. Track the time level by level, counting minutes until no more cells can be infected. If any uninfected cell remains, return -1.
Pro tip: Clarify edge cases upfront (e.g., no infected cells, all infected, unreachable regions) and mention that BFS is optimal because infection spreads uniformly in all directions at the same rate.
Restate the problem: each minute, infected cells spread to 4-directional neighbors. Identify edge cases: no infected cells, all cells infected, unreachable uninfected cells.
Recognize this as a multi-source BFS problem because infection spreads uniformly from multiple sources. BFS guarantees the minimum time.
Initialize a queue with all infected cells and count total uninfected cells. Process level by level, infecting neighbors and decrementing the uninfected count.
Increment time after each BFS level. After BFS, if uninfected count is zero, return time; otherwise return -1.
State time and space complexity: O(m*n) time and O(m*n) space. Walk through a small example to verify correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.