Sparse Plant Infection Simulation
Problem statement
You are given a sparse plant grid. Each row of plants is [row, column, state] for one plant at a unique signed 32-bit coordinate:
0means healthy;1means infected;2means immune;3means dead.
An unlisted coordinate contains no plant. Only listed plants participate in the simulation.
Day 0 is the supplied state. To advance from day t to day t + 1, read one start-of-day snapshot and apply all changes simultaneously:
- A healthy plant becomes infected when at least
thresholdof its eight neighboring listed plants are infected at the start of the day. - A newly infected plant first contributes to neighbor counts on the next day.
- An infected plant contributes for exactly
recoveryDaysconsecutive start-of-day states, including its first infected state, and then becomes immune at the next boundary. - Every initially infected plant starts with the full
recoveryDaysduration. - Immune and dead plants never change and never count as infected neighbors. Dead plants occur only in the supplied initial state.
Return the earliest nonnegative day after which no current or scheduled future transition can change any plant. An infected plant with remaining infectious days prevents stabilization even when the visible grid does not change on the next boundary.
Function
plantInfectionStabilizationDays(plants: int[][], threshold: int, recoveryDays: int) → intExamples
Example 1
plants = [[0,0,1],[0,1,0],[0,2,0]]threshold = 1recoveryDays = 2return = 4The infection moves one position to the right on days 1 and 2. The initially infected plant recovers on day 2, the middle plant on day 3, and the final plant on day 4. No future transition remains after day 4.
Example 2
plants = [[0,0,1],[0,1,0],[1,0,1],[1,1,0]]threshold = 2recoveryDays = 1return = 2Both healthy plants see the two initially infected plants in their eight-neighbor snapshots, so both become infected on day 1 while the original pair recovers. The new pair recovers on day 2.
Example 3
plants = [[0,0,0],[0,1,2],[1,0,3]]threshold = 1recoveryDays = 5return = 0There is no infected plant. The healthy plant cannot change, while the immune and dead plants are terminal, so the supplied day-0 state is already stable.
Constraints
1 <= plants.length <= 2 * 10^5.- Each
plants[i]is[row, column, state]; coordinates are unique signed 32-bit integers and0 <= state <= 3. 1 <= threshold <= 8.1 <= recoveryDays <= 10^9.- The stabilization day fits in a signed 32-bit integer.
- Updates are synchronous, use all eight neighboring coordinates, and only listed plants can change.