Minimize Malware Spread II

(This problem is the same as Minimize Malware Spread, with the differences bolded.)

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, completely removing it and any connections from this node to any other node.  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.

 


Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
Output: 1

Example 3:

Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
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

Solution:

The idea of this question is similar to leetcode 924 Minimize Malware Spread.


The key point of these two questions is to analyze how to change a node from an infected state to a safe one.



as picture shows, the yellow node is the initial infected node.
for the safe node [1,2,3,5,6], we analyze one by one.


we define node a are directly infected by node b if node a will be infected by node b without through any other infected node.


For node 1, it will be directly infected by node 0 and node 4,(0->1, 4->3->2->1)
For node 2, it is same as node 1(0->1->2, 4->3->2)
For node 3, it is same as node 1
For node 5, it is same as node 1
For node 6, it will be directly infected by node 4. (4 - > 6)


for node [1,2,3,5], even if we delete one node from the initial infected node, it will be infected by another node in the end.


So, a node may be safe if and only if it's just directly infected by one node. I called it s_node
we can use bfs solution to find the all s_node, and store the only node that can infect it.
Finally, we count which node in the stored node appears the most.



class Solution {
    public int minMalwareSpread(int[][] graph, int[] initial) {
        Map<Integer, List<Integer>> map = new HashMap<>();  //node -> initial nodes infect this node
        for (int start : initial) {
            Set<Integer> visited = fill(initial);
            Deque<Integer> queue = new ArrayDeque();
            queue.offer(start);
            while (!queue.isEmpty()) {
                int i = queue.poll();
                for (int j = 0; j < graph[i].length; j ++) {
                    if (graph[i][j] == 1 && visited.add(j)) {
                        List<Integer> canInfect = map.getOrDefault(j, new ArrayList());
                        canInfect.add(start);
                        map.put(j, canInfect);
                        queue.offer(j);
                    }
                }
            }
        }
        
        int[] res = new int[graph.length]; //node -> safe nodes it infects
        for (int node : map.keySet()) {
            if (map.get(node).size() == 1) {
                res[map.get(node).get(0)] ++;
            }
        }
        int ret = -1, max = 0;
        for (int i = 0; i < res.length; i ++) {
            if (res[i] > max || res[i] == max && ret > i) {
                max = res[i];
                ret = i;
            }
        }
        // System.out.println(Arrays.toString(res));
        Arrays.sort(initial);
        return ret == -1 ? initial[0] : ret;
    }
    
    private Set<Integer> fill(int[] arr) {
        Set<Integer> set = new HashSet();
        for (int i : arr) set.add(i);
        return set;
    }
}