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 new cells are infected, returning the maximum distance from any initial source to any reachable cell.
Pro tip: Clarify edge cases upfront: what if there are no initially infected plants? What if the grid is empty? Also, discuss how you would handle very large grids (e.g., using a queue with coordinate compression or processing in chunks) to show scalability awareness.
Ask about grid size limits, infection spread rules (4-directional or 8-directional), and whether diagonal spread is allowed. Confirm return value when no initial infection exists.
Recognize this as a multi-source BFS problem. Explain why BFS is optimal: it processes cells in order of increasing distance from any source, ensuring the first time a cell is reached is the shortest time.
Initialize a queue with all initially infected cells and set their distance to 0. While the queue is not empty, dequeue a cell, explore its uninfected neighbors, mark them infected, set their distance to current+1, and enqueue them.
Keep a variable for the maximum distance seen. After BFS completes, return that maximum. If there were no initial infections, return 0.
State time complexity O(n*m) and space O(n*m). Discuss edge cases: empty grid, no initial infection, all cells initially infected, and unreachable cells (if any).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.