Skip to content
Problem 749 · LeetCode 749(opens in a new tab)

Contain Virus

HardDepth-first search

A virus is spreading on an m × n grid, isInfected. A cell holding 1 is infected and a cell holding 0 is clean. You can build a wall on the edge between two side-by-side cells.

Each day you may wall in just one region (a group of infected cells joined up, down, left or right): the region that would infect the most clean cells tonight. You build a wall on every edge between that region and a clean cell, and the region never spreads again. Then, at night, every other region infects each clean cell next to it.

Repeat until no region can spread. Return the total number of walls built. The rules guarantee that each day exactly one region threatens the most cells.

Example 1

Input:  isInfected = [[1, 0, 0, 1, 0],
                      [0, 0, 0, 1, 1],
                      [0, 0, 0, 0, 0]]
Output: 10
Why:    day 1 walls the right region (6); day 2 the grown left one (4)
2 more examples and the constraints

Example 2

Input:  isInfected = [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]]
Output: 6
Why:    the two clean cells touch the ring along 6 edges

Example 3

Input:  isInfected = [[0, 0], [0, 0]]
Output: 0
Why:    there is no virus, so no walls are needed

Constraints

  • 1 ≤ m, n ≤ 50
  • Each cell is 0 or 1
  • On every day, one region threatens strictly more clean cells than any other

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Label, then scan per regionO(d·R·m·n)
  2. Collect frontiers while fillingO(d·m·n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 50 × 50 grid with many regionsVerdict
Label, then scan per regionO(d·R·m·n)
Correct; rescans per region
Collect frontiers while fillingO(d·m·n)
Best; one pass per day

m × n = grid size, d = number of days, R = regions on a day.

Edge cases to test

  • No virus. An all-0 grid needs 0 walls.
  • Everything infected. An all-1 grid threatens nothing, so 0 walls.
  • A clean cell touched twice. In Example 2 each clean cell touches the ring on 3 sides: 2 threatened cells, 6 walls.
  • Sealed regions. Walled-in cells (−1) are neither infected nor clean; they never spread and are never infected again.
  • The virus wins. If the grid fills up, stop and return the walls built so far.

Common mistakes

  • Counting threatened cells as walls; a cell can need up to 4 walls.
  • Letting the sealed region spread at night, or counting it in later days.
  • Spreading while still scanning, so a cell infected tonight joins a region the same day.

How to talk through it

  1. 1Clarify. Is a wall per edge or per cell? Do sealed regions ever matter again? Can there be ties?
  2. 2Describe the day loop. Find regions, pick the most threatening, seal it, spread the rest; repeat.
  3. 3Count while filling. During each DFS, collect the frontier set and count one wall per clean neighbour.
  4. 4Code the spread last. Infect the frontiers only after all regions are measured, and mark sealed cells −1.
  5. 5Test out loud. Run the ring example (2 cells, 6 walls) and the all-clean grid before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?