Pandemic Containment Strategy

Imagine you're leading a global health organization tasked with containing a rapidly spreading virus. The world is represented as an m ร— n grid where each cell can be either infected (value 1) or healthy (value 0).

๐Ÿฆ  The Challenge: Every night, the virus spreads to all neighboring cells (up, down, left, right) unless blocked by walls. You have limited resources and can only build walls around one infected region per day.

๐ŸŽฏ Your Strategy: Each day, identify all connected infected regions and determine which one threatens the most healthy cells the next night. Build walls around that entire region to contain it permanently.

๐Ÿ—๏ธ Wall Building: Walls are built on the boundaries between cells. For example, if an infected cell is adjacent to 3 healthy cells, you need 3 wall segments to protect those healthy cells.

Goal: Return the total number of wall segments needed to contain all infected regions before they spread further. If the entire world becomes infected, return the walls used during the process.

Input & Output

example_1.py โ€” Basic Grid
$ Input: [[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: Day 1: Two regions exist. Left region threatens 4 cells, right region threatens 4 cells. Since it's a tie, we choose the first one found. Build 7 walls around left region. Day 2: Right region threatens 4 cells, build 3 walls. Total: 10 walls.
example_2.py โ€” Single Region
$ Input: [[1,1,1],[1,0,1],[1,1,1]]
โ€บ Output: 4
๐Ÿ’ก Note: Only one infected region exists, forming a ring around a healthy cell. We need 4 walls to protect the center healthy cell from being infected.
example_3.py โ€” Already Contained
$ Input: [[1,1,1,0,0,0,0,0,0],[1,0,1,0,1,1,1,1,1],[1,1,1,0,0,0,0,0,0]]
โ€บ Output: 13
๐Ÿ’ก Note: Left region threatens 1 cell (center), right region threatens 4 cells. Day 1: Build 13 walls around right region. Day 2: Build 4 walls around left region. Total: 17 walls... Wait, let me recalculate: Day 1 builds walls around the more threatening right region (13 walls), then left region gets contained with 0 additional walls as it can't spread further.

Visualization

Tap to expand
Virus Containment Strategy VisualizationDay 1: Threat AssessmentABRegion A: Threatens 3 cellsRegion B: Threatens 4 cellsโ†’ Contain Region B (higher threat)Day 1: Build Walls7 walls built around Region B(Region B now contained - shown in gray)Day 2: Remaining SpreadRegion A spreads to 3 new cellsNow threatens 2 more cellsDay 3: Final Containment3 more walls builtTotal: 10 walls usedAlgorithm Summary1. Daily Simulation: Each iteration represents one day in the containment process2. Region Detection: Use DFS/BFS to find all connected infected areas3. Threat Calculation: Count healthy cells each region would infect tomorrow4. Strategic Priority: Always contain the most threatening region first5. Wall Counting: Sum perimeter segments needed for containment6. Spread Simulation: Remaining regions expand to adjacent cells
Understanding the Visualization
1
Scan for Outbreaks
Use DFS to identify all connected infected regions in the current grid state
2
Assess Threat Levels
For each region, count how many healthy cells would become infected tomorrow
3
Strategic Decision
Select the region that threatens the most healthy cells - this is our priority target
4
Build Containment
Calculate and build walls around the chosen region's perimeter (count wall segments)
5
Spread Simulation
Allow remaining uncontained regions to spread to adjacent healthy cells
6
Repeat Daily
Continue the process until all infected regions are contained or grid is fully infected
Key Takeaway
๐ŸŽฏ Key Insight: This is a greedy simulation problem where we must balance immediate containment with long-term strategy, always prioritizing regions that pose the greatest immediate threat to healthy areas.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(d ร— mยฒ ร— nยฒ)

d days ร— (mร—n grid traversal ร— mร—n regions to check)

n
2n
โš  Quadratic Growth
Space Complexity
O(m ร— n)

Additional space for visited arrays, region tracking, and grid copies

n
2n
โšก Linearithmic Space

Constraints

  • m == isInfected.length
  • n == isInfected[i].length
  • 1 โ‰ค m, n โ‰ค 50
  • isInfected[i][j] is either 0 or 1
  • There will always be a contiguous infected region that threatens the most uninfected cells on any given day
Asked in
Google 28 Amazon 15 Meta 12 Microsoft 8
71.2K Views
Medium Frequency
~35 min Avg. Time
1.8K 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