LeetCode-in-Java

924. Minimize Malware Spread

Hard

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:

Solution

public class Solution {
    private int[] size;
    private int[] par;

    public int minMalwareSpread(int[][] graph, int[] initial) {
        size = new int[graph.length];
        par = new int[graph.length];
        for (int i = 0; i < graph.length; i++) {
            size[i] = 1;
            par[i] = i;
        }
        // create groups
        for (int i = 0; i < graph.length; i++) {
            for (int j = 0; j < graph[0].length; j++) {
                if (graph[i][j] == 1) {
                    int p1 = find(i);
                    int p2 = find(j);
                    merge(p1, p2);
                }
            }
        }
        // no of infected in group
        int[] infected = new int[graph.length];
        for (int e : initial) {
            int p = find(e);
            infected[p]++;
        }
        int currSize = -1;
        int ans = -1;
        for (int e : initial) {
            int p = find(e);
            if (infected[p] == 1 && size[p] >= currSize) {
                if (size[p] > currSize) {
                    ans = e;
                } else {
                    ans = Math.min(ans, e);
                }
                currSize = size[p];
            }
        }
        // all groups have more than 1 infected node then return min value from initial
        if (ans == -1) {
            ans = initial[0];
            for (int j : initial) {
                ans = Math.min(ans, j);
            }
        }
        return ans;
    }

    private void merge(int p1, int p2) {
        if (p1 != p2) {
            if (size[p1] > size[p2]) {
                par[p2] = p1;
                size[p1] += size[p2];
            } else {
                par[p1] = p2;
                size[p2] += size[p1];
            }
        }
    }

    private int find(int u) {
        if (par[u] == u) {
            return u;
        }
        par[u] = find(par[u]);
        return par[u];
    }
}