Knew pretty quickly it was multi-source BFS, seeded from all infected cells at once.
Model the grid as a graph and use multi-source BFS starting from all initially infected cells simultaneously. Track the number of days (BFS levels) until no more cells can be infected, and after BFS, check if any healthy cells remain uninfected; if so, return -1.
Pro tip: Clarify edge cases upfront: if there are no healthy cells initially, return 0; if there are no infected cells and healthy cells exist, return -1. Also, mention that you can optimize space by reusing the grid to mark visited cells.
Restate the problem: infection spreads to 4-directional neighbors each day. Identify edge cases: no infected cells, no healthy cells, unreachable healthy cells (e.g., isolated by walls or boundaries).
Recognize this as a multi-source BFS problem where all initially infected cells are sources. BFS naturally simulates the day-by-day spread because it processes nodes level by level.
Initialize a queue with all infected cells and count healthy cells. For each day (BFS level), process all current infected cells, infect their healthy neighbors, add them to the queue, and decrement the healthy count. Increment days after each level.
After BFS, if the healthy count is greater than 0, return -1 because some healthy cells were never infected. Otherwise, return the number of days (or 0 if no healthy cells initially).
Time complexity: O(m*n) since each cell is processed once. Space complexity: O(m*n) for the queue in worst case. Mention that you can modify the grid in-place to avoid a separate visited set.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.