Infection Spread / Cellular Automata
You are given a two-dimensional grid that models an infection process. Each cell has one of three values:
0: healthy1: infected2: immune
Each day, every currently infected cell simultaneously infects its 4-directional healthy neighbors. Cells infected today can only start spreading the infection tomorrow. Immune cells never become infected and never spread the infection.
Return the number of days needed until every non-immune healthy cell is infected. If there is no initial infected cell, or if any healthy cell can never be reached because of immune cells, return -1. If every non-immune cell is already infected, return 0.
grid = [[1, 0, 0], [0, 2, 0], [0, 0, 0]] return = 4
The infection spreads around the immune center cell. The farthest healthy cell becomes infected after 4 days.
grid = [[1, 2, 0], [2, 0, 0], [0, 0, 0]] return = -1
The initial infected cell is blocked by immune cells, so the remaining healthy cells can never be infected.
grid = [[1, 2], [2, 1]] return = 0
There are no healthy non-immune cells to infect.
gridis a rectangular matrix.- Each cell is
0,1, or2. - Infection spreads only in the four orthogonal directions.
- Grid Infection Spread Until StablePHONE SCREEN · Seen May 2026
- Grid Infection with Immune Cells Until StablePHONE SCREEN · Seen May 2026
- Chat Event Counts in Recent WindowPHONE SCREEN · Seen May 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen May 2026
- ChatApp with BotsPHONE SCREEN · Seen May 2026
- IP Address to CIDR BlocksPHONE SCREEN · Seen May 2026
- Count Valid SequencesSeen Jan 2025
- Maximize The HitsSeen Sep 2024
public int timeToFullInfectionWithImmunity(int[][] grid) {
// write your code here
}