A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.

The world is modeled as an m x n binary grid isInfected, where:

  • isInfected[i][j] == 0 represents uninfected cells
  • isInfected[i][j] == 1 represents cells contaminated with the virus

A wall can be installed between any two 4-directionally adjacent cells, on the shared boundary.

Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited - each day, you can install walls around only one region (the affected area that threatens the most uninfected cells the following night).

Return the number of walls used to quarantine all the infected regions. If the world will become fully infected, return the number of walls used.

Input & Output

Example 1 — Basic Containment
$ Input: isInfected = [[0,1,0,0,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0]]
Output: 10
💡 Note: Two regions exist. Left region threatens 2 cells, right region threatens 4 cells. We contain the right region first with 10 walls, then the left spreads. Process continues until all contained.
Example 2 — Single Region
$ Input: isInfected = [[1,1,1],[1,0,1],[1,1,1]]
Output: 4
💡 Note: Single infected region surrounds one uninfected cell. We need 4 walls to completely isolate the center cell from infection.
Example 3 — No Infection
$ Input: isInfected = [[0,0,0],[0,0,0],[0,0,0]]
Output: 0
💡 Note: No infected cells exist, so no walls are needed.

Constraints

  • m == isInfected.length
  • n == isInfected[i].length
  • 1 ≤ m, n ≤ 50
  • isInfected[i][j] is either 0 or 1
  • There will never be a tie for the region that threatens the most uninfected cells

Visualization

Tap to expand
Contain Virus - BFS Approach INPUT isInfected Grid (4x8) = Infected (1) = Clean (0) Two Infected Regions: Region A: Left column (2 cells) Region B: Right column (3 cells) [0,1,0,0,0,0,0,1] [0,1,0,0,0,0,0,1] [0,0,0,0,0,0,0,1] [0,0,0,0,0,0,0,0] ALGORITHM STEPS 1 Find All Regions Use BFS to identify connected infected areas 2 Count Threats For each region, count adjacent uninfected cells 3 Quarantine Worst Build walls around region threatening most cells 4 Spread and Repeat Other regions spread, repeat until contained Day 1 Analysis: Region A threatens: 4 cells Region B threatens: 6 cells Quarantine Region B first! Walls needed: 10 FINAL RESULT Quarantined Grid = Quarantined = Wall boundary = Walls installed Total Walls 10 Wall Count Breakdown: Day 1: Quarantine Region B Walls = 3 + 3 + 3 + 1 = 10 Key Insight: Greedy strategy: Always quarantine the region threatening the MOST uninfected cells first. BFS explores each infected region, counts walls needed (edges to uninfected neighbors), and tracks threatened cells. After quarantine, remaining regions spread before next day. TutorialsPoint - Contain Virus | BFS Approach
Asked in
Google 8 Facebook 5
18.5K Views
Medium Frequency
~35 min Avg. Time
421 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen