Simulate Threshold Grid Infection
Simulate Threshold Grid Infection
You are provided with an m x n binary grid, where 1 represents an infected cell and 0 represents a healthy cell. You are also given an integer K.
The system evolves in discrete time steps, and all updates within a single step occur simultaneously:
- An already infected cell (
1) remains infected. - A healthy cell (
0) becomes infected if and only if exactlyKof its four orthogonal neighbors (up, down, left, right) were infected at the start of that specific time step. - Neighbors located outside the grid boundaries do not count towards the
Kthreshold.
Your task is to implement a function that calculates and returns the minimum number of time steps required for all cells in the grid to become infected. If the infection process reaches a stable state where no more cells can become infected, but some cells still remain healthy, the function should return -1.
You may assume that the input grid will always be non-empty.
AI interview prep
Core dilemma
Efficiently simulating a grid's state evolution with discrete, simultaneous updates, while correctly handling complex propagation rules ('exactly K neighbors') and robustly detecting termination (all infected or stable with healthy cells).
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Simulate Threshold Grid Infection
Simulate Threshold Grid Infection
You are provided with an m x n binary grid, where 1 represents an infected cell and 0 represents a healthy cell. You are also given an integer K.
The system evolves in discrete time steps, and all updates within a single step occur simultaneously:
- An already infected cell (
1) remains infected. - A healthy cell (
0) becomes infected if and only if exactlyKof its four orthogonal neighbors (up, down, left, right) were infected at the start of that specific time step. - Neighbors located outside the grid boundaries do not count towards the
Kthreshold.
Your task is to implement a function that calculates and returns the minimum number of time steps required for all cells in the grid to become infected. If the infection process reaches a stable state where no more cells can become infected, but some cells still remain healthy, the function should return -1.
You may assume that the input grid will always be non-empty.