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

Problem Statement of Minimize Malware Spread
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.
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.
Note that if a node was removed from the initial list of infected nodes, it might still be infected later due to the malware spread.
Example 1:
Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0
Example 2:
Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
Output: 0
Example 3:
Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
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), where n = |\texttt{graph}|
- Space Complexity: O(n)
924. Minimize Malware Spread LeetCode Solution in C++
class UnionFind {
public:
UnionFind(int n) : id(n), rank(n) {
iota(id.begin(), id.end(), 0);
}
void unionByRank(int u, int v) {
const int i = find(u);
const int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
}
int find(int u) {
return id[u] == u ? u : id[u] = find(id[u]);
}
private:
vector<int> id;
vector<int> rank;
};
class Solution {
public:
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
const int n = graph.size();
UnionFind uf(n);
vector<int> ufSize(n);
vector<int> malwareCount(n);
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (graph[i][j] == 1)
uf.unionByRank(i, j);
for (int i = 0; i < n; ++i)
++ufSize[uf.find(i)];
for (const int i : initial)
++malwareCount[uf.find(i)];
ranges::sort(initial);
int ans = initial[0];
int maxUfSize = 0;
// Find the maximum union's malware if it only contains 1 malware.
for (const int i : initial) {
const int id = uf.find(i);
if (ufSize[id] > maxUfSize && malwareCount[id] == 1) {
maxUfSize = ufSize[id];
ans = i;
}
}
return ans;
}
};
/* code provided by PROGIEZ */
924. Minimize Malware Spread LeetCode Solution in Java
class UnionFind {
public UnionFind(int n) {
id = new int[n];
rank = new int[n];
for (int i = 0; i < n; ++i)
id[i] = i;
}
public void unionByRank(int u, int v) {
final int i = find(u);
final int j = find(v);
if (i == j)
return;
if (rank[i] < rank[j]) {
id[i] = j;
} else if (rank[i] > rank[j]) {
id[j] = i;
} else {
id[i] = j;
++rank[j];
}
}
public int find(int u) {
return id[u] == u ? u : (id[u] = find(id[u]));
}
private int[] id;
private int[] rank;
}
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
final int n = graph.length;
UnionFind uf = new UnionFind(n);
int[] ufSize = new int[n];
int[] malwareCount = new int[n];
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (graph[i][j] == 1)
uf.unionByRank(i, j);
for (int i = 0; i < n; ++i)
++ufSize[uf.find(i)];
for (final int i : initial)
++malwareCount[uf.find(i)];
Arrays.sort(initial);
int ans = initial[0];
int maxUfSize = 0;
// Find the maximum union's malware if it only contains 1 malware.
for (final int i : initial) {
final int id = uf.find(i);
if (ufSize[id] > maxUfSize && malwareCount[id] == 1) {
maxUfSize = ufSize[id];
ans = i;
}
}
return ans;
}
}
// code provided by PROGIEZ
924. Minimize Malware Spread 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.