928. Minimize Malware Spread II LeetCode Solution
In this guide, you will get 928. Minimize Malware Spread II LeetCode Solution with the best time and space complexity. The solution to Minimize Malware Spread II 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
- Minimize Malware Spread II solution in C++
- Minimize Malware Spread II solution in Java
- Minimize Malware Spread II solution in Python
- Additional Resources

Problem Statement of Minimize Malware Spread II
You are given a network of n nodes represented as an n x n adjacency matrix graph, where the ith node is directly connected to the jth node if graph[i][j] == 1.
Some nodes initial are initially infected by malware. Whenever two nodes are directly connected, and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.
Suppose M(initial) is the final number of nodes infected with malware in the entire network after the spread of malware stops.
We will remove exactly one node from initial, completely removing it and any connections from this node to any other node.
Return the node that, if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.
Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0
Example 2:
Input: graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
Output: 1
Example 3:
Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
Output: 1
Constraints:
n == graph.length
n == graph[i].length
2 <= n <= 300
graph[i][j] is 0 or 1.
graph[i][j] == graph[j][i]
graph[i][i] == 1
1 <= initial.length < n
0 <= initial[i] <= n – 1
All the integers in initial are unique.
Complexity Analysis
- Time Complexity: O(n^2 \cdot |\texttt{initial}|)
- Space Complexity: O(n)
928. Minimize Malware Spread II LeetCode Solution in C++
class Solution {
public:
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int ans = 0;
int minCount = graph.size();
ranges::sort(initial);
for (const int i : initial) {
const int count = bfs(graph, i, initial);
if (count < minCount) {
minCount = count;
ans = i;
}
}
return ans;
}
private:
int bfs(const vector<vector<int>>& graph, int removed, vector<int>& initial) {
queue<int> q;
vector<bool> seen(graph.size());
seen[removed] = true;
int count = 0;
for (const int i : initial)
if (i != removed) {
q.push(i);
seen[i] = true;
}
while (!q.empty()) {
const int u = q.front();
q.pop();
++count;
for (int i = 0; i < graph.size(); ++i) {
if (seen[i])
continue;
if (i != u && graph[i][u]) {
q.push(i);
seen[i] = true;
}
}
}
return count;
}
};
/* code provided by PROGIEZ */
928. Minimize Malware Spread II LeetCode Solution in Java
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
int ans = 0;
int minCount = graph.length;
Arrays.sort(initial);
for (final int i : initial) {
final int count = bfs(graph, i, initial);
if (count < minCount) {
minCount = count;
ans = i;
}
}
return ans;
}
private int bfs(int[][] graph, int removed, int[] initial) {
Queue<Integer> q = new ArrayDeque<>();
boolean[] seen = new boolean[graph.length];
seen[removed] = true;
int count = 0;
for (final int i : initial)
if (i != removed) {
q.offer(i);
seen[i] = true;
}
while (!q.isEmpty()) {
final int u = q.poll();
++count;
for (int i = 0; i < graph.length; ++i) {
if (seen[i])
continue;
if (i != u && graph[i][u] == 1) {
q.offer(i);
seen[i] = true;
}
}
}
return count;
}
}
// code provided by PROGIEZ
928. Minimize Malware Spread II 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.