Grid Infection
Instructions
A research lab is monitoring a plant disease spreading across an m x n grid grid, where each cell is either healthy (0) or infected (1).
The disease spreads in simultaneous daily rounds. Each day, every healthy cell that currently has at least threshold infected cells among its 8 neighbors (including diagonals) becomes infected. All of a day's new infections are applied at once — cells infected today do not help infect other cells until tomorrow. Once infected, a cell stays infected forever.
Return the number of days that pass until the disease can no longer spread. If no healthy cell can be infected on the first day, return 0.
Example 1:
Input: grid = [[1, 1],
[0, 0]], threshold = 2
Output: 1
Explanation: Both bottom cells see 2 infected neighbors, so both become infected on day 1. On day 2 nothing changes, so the answer is 1.
Example 2:
Input: grid = [[1, 0, 0],
[1, 0, 0],
[0, 0, 0]], threshold = 2
Output: 3
Explanation: Day 1: (0,1) and (1,1) each see both initial infected cells and become infected. Day 2: (0,2), (1,2), (2,0), and (2,1) now each have at least 2 infected neighbors. Day 3: (2,2) becomes infected. Day 4: no change, so the answer is 3.
Example 3:
Input: grid = [[1, 0],
[0, 0]], threshold = 2
Output: 0
Explanation: No healthy cell ever has 2 infected neighbors, so the disease never spreads.
Constraints:
1 <= m, n <= 500grid[i][j]is0or11 <= threshold <= 8
Function Signature
Online Judge
Result will appear here after submission.
