This is LC 994 but they kept adding rules until it barely resembled the original.
Model the grid as a state machine with three states (healthy, infected, immune) and simulate day-by-day using a queue or BFS to track newly infected cells. Track for each infected cell the number of days it has been infected and the count of infected neighbors to determine when it becomes severely infected and when it dies. Continue until no state changes occur, counting the total days and dead plants.
Pro tip: Clarify the exact rules upfront: whether immunity is permanent, if infected cells can recover, and how the neighbor count is computed (e.g., 4-directional vs 8-directional). Also, consider edge cases like initial infected cells and whether they can become severely infected immediately.
Ask clarifying questions to confirm the state transitions, neighbor definition, and the two parameters (threshold for severe infection and days until death). Ensure you understand the initial grid setup and what constitutes 'nothing changes'.
Use a 2D array to represent the grid states. Maintain auxiliary structures: a queue for BFS to process newly infected cells, and dictionaries or arrays to track infection duration and neighbor counts for each infected cell.
At each day, process all infected cells: update their neighbor counts, check if they become severely infected (based on threshold), and if severely infected for the required days, mark them as dead. Then spread infection to healthy neighbors of infected cells (if not immune).
After each day, check if any state changed. If no changes, stop and return the number of days elapsed and the total dead plants. Otherwise, increment the day counter and continue.
Discuss time and space complexity: O(days * cells) in naive simulation, but can be optimized using BFS with a queue to process only affected cells. Mention potential optimizations for large grids.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.