This question evaluates a candidate's ability to reason about grid-based state propagation, algorithmic problem solving, and performance considerations when managing multi-step updates across a 2D matrix.
You are given an m x n grid representing the state of individuals in a lab.
0
= empty cell (no one)
1
= healthy individual
2
= infected individual
Every minute, any healthy individual (1) that is 4-directionally adjacent (up/down/left/right) to an infected individual (2) becomes infected.
Return the minimum number of minutes needed until no healthy individuals remain. If it is impossible to infect all healthy individuals, return -1. If there are no healthy individuals initially, return 0.
grid
of size
m x n
.
-1
if impossible.
1 ≤ m, n ≤ 200
grid[i][j] ∈ {0,1,2}