This was the fourth layer on the same grid problem and by this point my solution was getting messy.
Model the infection spread as a multi-source BFS where each cell's infection time is the minimum Manhattan distance to an initial infected cell. Then, for each non-infected cell, determine the day it dies by checking when it accumulates at least K infected neighbors, and simulate the process day by day until no infected cells remain, tracking the total days and deaths.
Pro tip: Clarify the problem constraints and edge cases upfront (e.g., K=0, D=0, multiple initial infected cells) to avoid incorrect assumptions, and discuss how you would optimize the simulation for large grids using event-driven updates or priority queues.
Ask about grid size, K, D, initial infected cells, and whether infection spreads to neighbors or only initial infected cells cause deaths. Confirm the definition of 'neighbor' (4-directional or 8-directional).
Use multi-source BFS from all initially infected cells to compute the earliest day each cell becomes infected. This gives the infection time for every cell.
For each cell, count how many infected neighbors it has at each day. The cell dies on the first day when the count reaches K, provided it is not already infected. If it never reaches K, it survives.
Iterate day by day, updating the grid: remove cells that die, and possibly spread infection if applicable. Continue until no infected cells remain, tracking total days and deaths.
Return (total_days, total_deaths). Mention potential optimizations like using a priority queue for death events or early termination when no changes occur.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.