Model the spread as a multi-source BFS where all initially infected cells are enqueued at time 0. Process the grid level by level, incrementing time after each level, and track the number of healthy cells infected. After BFS, if any healthy cell remains, return -1; otherwise return the total minutes elapsed.
Pro tip: Clarify that you're treating this as a shortest-path problem on an unweighted grid, and mention that you can optimize space by modifying the grid in place or using a visited set. Also, handle edge cases like no initial infected cells or an already fully infected grid.
Restate the problem to confirm it's a multi-source BFS on a grid. Identify edge cases: empty grid, no infected cells, all infected, unreachable healthy cells.
Scan the grid to enqueue all initially infected cells and count healthy cells. Use a queue for BFS and a variable to track remaining healthy cells.
While the queue is not empty, process all nodes at the current level, infecting adjacent healthy cells, marking them infected, decrementing the healthy count, and enqueueing them. Increment time after each level.
After BFS, if the healthy count is zero, return the elapsed time; otherwise return -1. Discuss time and space complexity: O(m*n) time and O(m*n) space in worst case.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.