My first instinct was plain BFS and I started coding before fully thinking through the immunity part.
Model the grid as a graph and simulate the infection spread day by day using BFS, tracking each infected cell's recovery day. The process ends when no healthy cells are adjacent to infected cells and all infected cells have recovered.
Pro tip: Clarify edge cases upfront: what if there are no infected cells initially? Also, mention that you can optimize by stopping simulation early if no new infections occur and all infected have recovered.
Ask about grid size, initial states, D value, and whether immunity is permanent. Confirm that recovery happens after exactly D days and that infected can spread each day before recovery.
Represent the grid as a 2D array and use a queue for BFS. Track infection day and recovery day for each cell, or maintain separate sets for healthy, infected, and immune.
For each day, process all currently infected cells: they infect adjacent healthy cells (to be infected next day) and if their infection duration reaches D, they become immune. Use a queue to manage the order.
Stop when no new infections occur and all infected cells have recovered. The answer is the number of days simulated until no state changes are possible.
Time complexity is O(N*M) since each cell is processed at most once. Space complexity is O(N*M) for the grid and queue.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.