FastPrepDays Until Poisoned Plants Stabilize

Days Until Poisoned Plants Stabilize

Apple logoApple● MediumFULLTIMEPHONE SCREEN
Learn

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) → int

Examples

Example 1

grid = [[1,0,0],[0,0,0],[0,0,0]]k = 1return = 2

The 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 = 0

No healthy plant initially has two poisoned neighbors, so the grid is already stable.

Constraints

  • 1 <= grid.length, grid[i].length <= 500.
  • Every cell is 0 or 1, and every row has the same length.
  • 1 <= k <= 8.

More Apple problems

See Apple hiring insights
public int daysUntilPlantsStabilize(int[][] grid, int k) {
    // Write your code here.
}
grid[[1,0,0],[0,0,0],[0,0,0]]
k1
expected2
Checking account…