LeetCode 924: Minimize Malware Spread Solution
Master LeetCode problem 924 (Minimize Malware Spread), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
924. Minimize Malware Spread
Problem Explanation
Explanation
To solve this problem, we need to simulate the spread of malware starting from the initially infected nodes. We can use a union-find data structure to keep track of the connected components and their sizes. By iterating through each node in the initial infected nodes and simulating the spread of malware, we can calculate the final infected nodes for each removal of an initially infected node. The one node that, when removed, minimizes the number of infected nodes is our answer.
- Initialize an array
parentto keep track of the parent node of each node in the graph. - Initialize an array
sizeto keep track of the size of each connected component. - Implement the
findandunionfunctions for the union-find data structure. - Simulate the spread of malware for each initial infected node, keeping track of the total infected nodes for each removal.
- Return the node that, when removed, minimizes the number of infected nodes.
Time complexity: O(n^2) where n is the number of nodes in the graph. Space complexity: O(n)
Solution Code
class Solution {
public int minMalwareSpread(int[][] graph, int[] initial) {
int n = graph.length;
int[] parent = new int[n];
int[] size = new int[n];
Arrays.fill(size, 1);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (graph[i][j] == 1) {
union(parent, size, i, j);
}
}
}
int[] infected = new int[n];
for (int node : initial) {
int root = find(parent, node);
infected[root]++;
}
int[] total = new int[n];
for (int node : initial) {
int root = find(parent, node);
if (infected[root] == 1) {
total[root] = size[root];
}
}
int ans = Integer.MAX_VALUE;
int minInfection = Integer.MIN_VALUE;
for (int node : initial) {
int root = find(parent, node);
if (total[root] > 0) {
if (infected[root] == 1) {
if (minInfection < infected[root] || (minInfection == infected[root] && node < ans)) {
ans = node;
minInfection = infected[root];
}
} else {
if (minInfection < infected[root]) {
ans = node;
minInfection = infected[root];
}
}
}
}
return ans;
}
private int find(int[] parent, int x) {
if (parent[x] != x) {
parent[x] = find(parent, parent[x]);
}
return parent[x];
}
private void union(int[] parent, int[] size, int x, int y) {
int rootX = find(parent, x);
int rootY = find(parent, y);
if (rootX != rootY) {
parent[rootX] = rootY;
size[rootY] += size[rootX];
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 924 (Minimize Malware Spread)?
This page provides optimized solutions for LeetCode problem 924 (Minimize Malware Spread) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 924 (Minimize Malware Spread)?
The time complexity for LeetCode 924 (Minimize Malware Spread) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 924 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 924 in Java, C++, or Python.