My first instinct was multi-source BFS and that direction was right, but K not being 1 threw me off.
Model the infection spread as a multi-source BFS where each cell's infection time is determined by when it accumulates at least K infected neighbors. Use a priority queue to process cells in order of infection time, updating neighbor counts and enqueueing cells when their count reaches K. If all cells become infected, return the maximum infection time; otherwise, return -1.
Pro tip: Clarify edge cases upfront: K=0 means all cells are infected at time 0, and if no initial infected cells exist and K>0, return -1 immediately. Also, mention that the BFS can be optimized by only tracking healthy cells' neighbor counts.
Restate the problem to ensure clarity: grid size, infection rule, and goal. Discuss edge cases like K=0, no initial infected cells, and K greater than max possible neighbors.
Recognize this as a multi-source BFS with a threshold condition. Explain why a simple BFS won't work directly and why a priority queue (or bucket queue) is needed to process cells in order of infection time.
Use a 2D array to store infection times (or -1 for uninfected). Maintain a count of infected neighbors for each healthy cell, and a priority queue (min-heap) of cells that will become infected, keyed by time.
Initialize the queue with all initially infected cells at time 0. While the queue is not empty, pop the cell with the smallest time, and for each healthy neighbor, increment its infected neighbor count; if it reaches K, compute its infection time (current time + 1) and push it into the queue.
After the simulation, check if all cells are infected. If yes, return the maximum infection time; otherwise, return -1. Discuss time and space complexity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.