Multi-source BFS from all starting X cells, track the max depth, done.
Model the grid as a graph and use multi-source BFS starting from all initially infected cells simultaneously. Track the number of BFS layers (days) until no new cells are infected, then return that count.
Pro tip: Clarify edge cases upfront: if there are no infected cells, the answer is 0; if all cells are infected, also 0. Also, mention that you can optimize space by modifying the grid in-place or using a queue of coordinates.
Restate the problem: infection spreads to all 8 neighbors each day. We need the number of days until no more spread. Confirm that diagonal spread is allowed and that we count full days.
Recognize this as a multi-source BFS problem. All initially infected cells are sources at day 0. Each BFS layer represents one day of spread.
Initialize a queue with all infected cells. For each day, process all cells currently in the queue (snapshot the size), and for each, check all 8 neighbors. If a neighbor is healthy, infect it and add to queue. Increment day count after each layer.
Stop when the queue is empty. Return the number of days elapsed. Handle cases with no infected cells (return 0) and all infected cells (return 0).
Time complexity: O(R*C) since each cell is processed once. Space complexity: O(R*C) for the queue in worst case.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.