Contain Virus - Problem
Pandemic Containment Strategy
Imagine you're leading a global health organization tasked with containing a rapidly spreading virus. The world is represented as an
๐ฆ 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.
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
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)
โ Quadratic Growth
Space Complexity
O(m ร n)
Additional space for visited arrays, region tracking, and grid copies
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code