Ctrl K

Infection Spread Time

Medium
Computers in a room are laid out as an m × n grid field. Each cell holds one of three values: - 0: the spot is empty; - 1: a healthy computer; - 2: an infected computer. Every minute the virus jumps from each infected computer to its healthy neighbours above, below, left and right (not diagonally). Return how many minutes pass until no healthy computer is left. If some computer can never be infected, return -1.
Example 1
field=[[1, 1, 0], [2, 1, 1], [0, 0, 1]] → 3

Minute 1: the neighbours of (1,0), cells (0,0) and (1,1), get infected. Minute 2: (0,1) and (1,2). Minute 3: (2,2). Everyone is infected after 3 minutes.

Example 2
field=[[2, 0, 1]] → -1

An empty cell separates the healthy computer from the infected one, so the virus never reaches it.

Example 3
field=[[0, 2], [2, 0]] → 0

There are no healthy computers from the start, so no waiting is needed.

Constraints
  • 1 ≤ m, n ≤ 100
  • field[i][j] is 0, 1 or 2
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDrain the Landlocked Ponds MediumLand Regions on a Map MediumSteps to the Nearest Exit MediumRain Reaching Both Seas MediumCan the Ball Stop at the Target Medium
loading Python…
Input
field=[[1, 1, 0], [2, 1, 1], [0, 0, 1]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.