Formatted question description: https://leetcode.ca/all/924.html

924. Minimize Malware Spread (Hard)

In a network of nodes, each node i is directly connected to another node j if and only 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 one node from the initial list.  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 may still be infected later as a result of 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

 

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

Companies:
Dropbox, Amazon

Related Topics:
Depth-first Search, Union Find

Solution 1. Union Find

Use UnionFind to count the infected nodes given initial array and a skipped initial node. Find the skipped initial node that results in minimum infected count.

// OJ: https://leetcode.com/problems/minimize-malware-spread/

// Time: O(N^2)
// Space: O(N)
class UnionFind {
    vector<int> id, size;
public:
    UnionFind(int n) : id(n), size(n, 1) {
        for (int i = 0; i < n; ++i) id[i] = i;
    }
    void connect(int a, int b) {
        int x = find(a), y = find(b);
        if (x == y) return;
        id[x] = y;
        size[y] += size[x];
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
    int getSize(int a) {
        return size[find(a)];
    }
};
class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int N = graph.size(), ans = initial[0], minInfected = INT_MAX;
        sort(initial.begin(), initial.end());
        UnionFind uf(N);
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j < N; ++j) {
                if (graph[i][j]) uf.connect(i, j);
            }
        }
        for (int i = 0; i < initial.size(); ++i) {
            int cnt = 0;
            unordered_set<int> s;
            for (int j = 0; j < initial.size(); ++j) {
                if (j == i) continue;
                int r = uf.find(initial[j]);
                if (s.count(r)) continue;
                s.insert(r);
                cnt += uf.getSize(r);
            }
            if (cnt < minInfected) {
                ans = initial[i];
                minInfected = cnt;
            }
        }
        return ans;
    }
};

Solution 2. Union Find

Keep looking for the initial node that belongs to the largest connected component with only one initial node in it. If there is no such a node, return the smallest index of initial nodes.

// OJ: https://leetcode.com/problems/minimize-malware-spread/

// Time: O(N^2)
// Space: O(N)
class UnionFind {
private:
    vector<int> rank, id, size;
public:
    UnionFind(int n): rank(n, 1), id(n), size(n, 1) {
        for (int i = 0; i < n; ++i) id[i] = i;
    }
    int find(int p) {
        if (id[p] == p) return p;
        return id[p] = find(id[p]);
    }
    void connect(int p, int q) {
        int a = find(p), b = find(q);
        if (a == b) return;
        if (rank[a] > rank[b]) {
            id[b] = a;
            size[a] += size[b];
        } else {
            if (rank[a] == rank[b]) rank[b]++;
            id[a] = b;
            size[b] += size[a];
        }
    }
    int getUnionSize(int p) {
        return size[find(p)];
    }
};
class Solution {
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        sort(initial.begin(), initial.end());
        int ans = initial[0], maxSize = 0, N = graph.size();
        UnionFind uf(N);
        for (int i = 0; i < N; ++i) {
            for (int j = i + 1; j < N; ++j) {
                if (graph[i][j]) uf.connect(i, j);
            }
        }
        unordered_map<int, int> m;
        for (int n : initial) m[uf.find(n)]++;
        for (int n : initial) {
            int size = uf.getUnionSize(n);
            if (m[uf.find(n)] == 1 && size > maxSize) {
                ans = n;
                maxSize = size;
            }
        }
        return ans;
    }
};

Solution 3. Component Coloring (DFS)

// OJ: https://leetcode.com/problems/minimize-malware-spread/

// Time: O(N^2)
// Space: O(N)
class Solution {
    void dfs(vector<vector<int>> &graph, int start, int c, vector<int> &color) {
        color[start] = c;
        for (int i = 0; i < graph.size(); ++i) {
            if (graph[start][i] && !color[i]) dfs(graph, i, c, color);
        }
    }
public:
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        unordered_set<int> init(initial.begin(), initial.end());
        sort(initial.begin(), initial.end());
        int N = graph.size(), c = 0, ans = initial[0], maxSize = 0;
        vector<int> color(N);
        for (int n : initial) {
            if (color[n]) continue;
            dfs(graph, n, ++c, color);
        }
        for (int i = 1; i <= c; ++i) {
            int cnt = 0, mal = INT_MAX, malCnt = 0;
            for (int j = 0; j < N; ++j) {
                if (color[j] == i) {
                    cnt++;
                    if (init.count(j)) {
                        mal = min(mal, j);
                        ++malCnt;
                    }
                }
            }
            if (malCnt == 1 && cnt > maxSize) {
                maxSize = cnt;
                ans = mal;
            }
        }
        return ans;
    }
};

Java

class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        int remove = -1;
        int maxDecrease = 0;
        Arrays.sort(initial);
        int length = graph.length;
        Set<Integer> initialSet = new HashSet<Integer>();
        for (int infected : initial)
            initialSet.add(infected);
        Set<Integer> excludeSet = new HashSet<Integer>();
        Queue<Integer> queue = new LinkedList<Integer>();
        for (int infected : initial) {
            if (!excludeSet.contains(infected)) {
                boolean single = true;
                boolean[] visited = new boolean[length];
                visited[infected] = true;
                queue.offer(infected);
                int count = 1;
                while (!queue.isEmpty()) {
                    int node = queue.poll();
                    for (int i = 0; i < length; i++) {
                        if (graph[node][i] == 1 && !visited[i]) {
                            visited[i] = true;
                            queue.offer(i);
                            if (initialSet.contains(i)) {
                                single = false;
                                excludeSet.add(i);
                            }
                            count++;
                        }
                    }
                }
                if (single) {
                    if (count > maxDecrease) {
                        maxDecrease = count;
                        remove = infected;
                    }
                } else {
                    excludeSet.add(infected);
                    if (remove == -1)
                        remove = infected;
                }
            }
        }
        return remove;
    }
}

All Problems

All Solutions