Days Until Poisoned Plants Stabilize
Problem statement
A rectangular binary grid represents plants: 1 is poisoned and 0 is healthy. At the end of each day, every healthy plant with at least k poisoned neighbors becomes poisoned. The eight horizontally, vertically, and diagonally adjacent cells are neighbors.
All changes for a day are simultaneous. Return the number of days until a full day would produce no additional poisoned plants.
Function
daysUntilPlantsStabilize(grid: int[][], k: int) → intExamples
Example 1
grid = [[1,0,0],[0,0,0],[0,0,0]]k = 1return = 2The cells adjacent to the initial poison change on day 1, and the final distant cell changes on day 2.
Example 2
grid = [[1,0],[0,0]]k = 2return = 0No healthy plant initially has two poisoned neighbors, so the grid is already stable.
Constraints
1 <= grid.length, grid[i].length <= 500.- Every cell is
0or1, and every row has the same length. 1 <= k <= 8.