749. Contain Virus LeetCode Solution
In this guide, you will get 749. Contain Virus LeetCode Solution with the best time and space complexity. The solution to Contain Virus problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Contain Virus solution in C++
- Contain Virus solution in Java
- Contain Virus solution in Python
- Additional Resources

Problem Statement of Contain Virus
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, and isInfected[i][j] == 1 represents cells contaminated with the virus. A wall (and only one 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 (i.e., the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night). There will never be a tie.
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.
Example 1:
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
Explanation: There are 2 contaminated regions.
On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is:
On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
Example 2:
Input: isInfected = [[1,1,1],[1,0,1],[1,1,1]]
Output: 4
Explanation: Even though there is only one cell saved, there are 4 walls built.
Notice that walls are only built on the shared boundary of two different cells.
Example 3:
Input: isInfected = [[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
Explanation: The region on the left only builds two new walls.
Constraints:
m == isInfected.length
n == isInfected[i].length
1 <= m, n <= 50
isInfected[i][j] is either 0 or 1.
There is always a contiguous viral region throughout the described process that will infect strictly more uncontaminated squares in the next round.
Complexity Analysis
- Time Complexity: O(m^2n^2)
- Space Complexity: O(mn)
749. Contain Virus LeetCode Solution in C++
struct Region {
// Given m = the number of rows and n = the number of columns, (x, y) will be
// hashed as x * n + y.
unordered_set<int> infected;
unordered_set<int> noninfected;
int wallsRequired = 0;
};
class Solution {
public:
int containVirus(vector<vector<int>>& isInfected) {
const int m = isInfected.size();
const int n = isInfected[0].size();
int ans = 0;
while (true) {
vector<Region> regions;
vector<vector<bool>> seen(m, vector<bool>(n));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (isInfected[i][j] == 1 && !seen[i][j]) {
Region region;
// Use DFS to find all the regions (1s).
dfs(isInfected, i, j, region, seen);
if (!region.noninfected.empty())
regions.push_back(region);
}
if (regions.empty())
break; // No region causes further infection.
// Regions that infect the most neighbors will be sorted to the back of
// the array.
ranges::sort(regions, ranges::less{}, [](const Region& region) {
return region.noninfected.size();
});
// Build walls around the region that infects the most neighbors.
Region mostInfectedRegion = regions.back();
regions.pop_back();
ans += mostInfectedRegion.wallsRequired;
for (const int neighbor : mostInfectedRegion.infected) {
const int i = neighbor / n;
const int j = neighbor % n;
// The isInfected is now contained and won't be infected anymore.
isInfected[i][j] = 2;
}
// For remaining regions, infect their neighbors.
for (const Region& region : regions)
for (const int neighbor : region.noninfected) {
const int i = neighbor / n;
const int j = neighbor % n;
isInfected[i][j] = 1;
}
}
return ans;
}
private:
void dfs(const vector<vector<int>>& isInfected, int i, int j, Region& region,
vector<vector<bool>>& seen) {
if (i < 0 || i == isInfected.size() || j < 0 || j == isInfected[0].size())
return;
if (seen[i][j] || isInfected[i][j] == 2)
return;
if (isInfected[i][j] == 0) {
region.noninfected.insert(i * isInfected[0].size() + j);
++region.wallsRequired;
return;
}
// isInfected[i][j] == 1
seen[i][j] = true;
region.infected.insert(i * isInfected[0].size() + j);
dfs(isInfected, i + 1, j, region, seen);
dfs(isInfected, i - 1, j, region, seen);
dfs(isInfected, i, j + 1, region, seen);
dfs(isInfected, i, j - 1, region, seen);
}
};
/* code provided by PROGIEZ */
749. Contain Virus LeetCode Solution in Java
class Region {
// Given m = the number of rows and n = the number of columns, (x, y) will be
// hashed as x * n + y.
public Set<Integer> infected = new HashSet<>();
public Set<Integer> noninfected = new HashSet<>();
public int wallsRequired = 0;
}
class Solution {
public int containVirus(int[][] isInfected) {
final int m = isInfected.length;
final int n = isInfected[0].length;
int ans = 0;
while (true) {
List<Region> regions = new ArrayList<>();
boolean[][] seen = new boolean[m][n];
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (isInfected[i][j] == 1 && !seen[i][j]) {
Region region = new Region();
// Use DFS to find all the regions (1s).
dfs(isInfected, i, j, region, seen);
if (!region.noninfected.isEmpty())
regions.add(region);
}
if (regions.isEmpty())
break; // No region causes further infection.
// Regions that infect the most neighbors will be sorted to the back of
// the array.
Collections.sort(regions, (a, b) -> a.noninfected.size() - b.noninfected.size());
// Build walls around the region that infects the most neighbors.
Region mostInfectedRegion = regions.get(regions.size() - 1);
regions.remove(regions.size() - 1);
ans += mostInfectedRegion.wallsRequired;
for (final int neighbor : mostInfectedRegion.infected) {
final int i = neighbor / n;
final int j = neighbor % n;
// The isInfected is now contained and won't be infected anymore.
isInfected[i][j] = 2;
}
// For remaining regions, infect their neighbors.
for (final Region region : regions)
for (final int neighbor : region.noninfected) {
final int i = neighbor / n;
final int j = neighbor % n;
isInfected[i][j] = 1;
}
}
return ans;
}
private void dfs(int[][] isInfected, int i, int j, Region region, boolean[][] seen) {
if (i < 0 || i == isInfected.length || j < 0 || j == isInfected[0].length)
return;
if (seen[i][j] || isInfected[i][j] == 2)
return;
if (isInfected[i][j] == 0) {
region.noninfected.add(i * isInfected[0].length + j);
++region.wallsRequired;
return;
}
// isInfected[i][j] == 1
seen[i][j] = true;
region.infected.add(i * isInfected[0].length + j);
dfs(isInfected, i + 1, j, region, seen);
dfs(isInfected, i - 1, j, region, seen);
dfs(isInfected, i, j + 1, region, seen);
dfs(isInfected, i, j - 1, region, seen);
}
}
// code provided by PROGIEZ
749. Contain Virus LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.