I'd done the orange rotting problem before so the structure clicked pretty fast.
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 cell remains uninfected; if so, return -1, else return the maximum time.
Pro tip: Explicitly discuss how you handle edge cases like no healthy cells (return 0) and multiple initial infections, and mention that BFS ensures the minimum time because it explores level by level.
Clarify that infection spreads to 4-directional neighbors each minute, and we need the minimum minutes to infect all healthy cells. Identify edge cases: no healthy cells, no infected cells, unreachable healthy cells.
Add all initially infected cells to a queue with time 0, and count the total number of healthy cells. This count will help determine if all healthy cells get infected.
While the queue is not empty, process each level (minute) by dequeuing all cells at the current time, and for each, check its 4 neighbors. If a neighbor is healthy, infect it, decrement the healthy count, and enqueue it with time+1.
After BFS, if the healthy count is greater than 0, return -1 (some healthy cells are unreachable). Otherwise, return the maximum time recorded during BFS (or 0 if no healthy cells initially).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.