Sign in to devexcode.com with google.com

To continue, google.com will share your name, email address, and profile picture with this site. See this site's privacy policy.

924. Minimize Malware Spread

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.

  1. Initialize an array parent to keep track of the parent node of each node in the graph.
  2. Initialize an array size to keep track of the size of each connected component.
  3. Implement the find and union functions for the union-find data structure.
  4. Simulate the spread of malware for each initial infected node, keeping track of the total infected nodes for each removal.
  5. 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)

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];
        }
    }
}

Code Editor (Testing phase)

Improve Your Solution

Use the editor below to refine the provided solution. Select a programming language and try the following:

  • Add import statement if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.