LeetCode-in-Java

928. Minimize Malware Spread II

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, 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

Constraints:

Solution

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Solution {
    private final Map<Integer, ArrayList<Integer>> adj = new HashMap<>();
    private Set<Integer> visited;
    private int count = 0;

    private void bfs(int ind, int[] initial) {
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < initial.length; i++) {
            if (i != ind) {
                q.add(initial[i]);
                visited.add(initial[i]);
            }
        }
        while (!q.isEmpty()) {
            int curr = q.poll();
            if (curr != initial[ind]) {
                count++;
            }
            ArrayList<Integer> children = adj.get(curr);
            if (children != null) {
                for (int child : children) {
                    if (!visited.contains(child)) {
                        q.add(child);
                        visited.add(child);
                    }
                }
            }
        }
    }

    public int minMalwareSpread(int[][] graph, int[] initial) {
        int n = graph.length;
        for (int i = 0; i < n; i++) {
            adj.putIfAbsent(i, new ArrayList<>());
            for (int j = 0; j < n; j++) {
                if (graph[i][j] == 1) {
                    adj.get(i).add(j);
                }
            }
        }
        int min = n + 1;
        Arrays.sort(initial);
        int node = initial[0];
        for (int i = 0; i < initial.length; i++) {
            visited = new HashSet<>();
            ArrayList<Integer> children = adj.get(initial[i]);
            adj.remove(initial[i]);
            bfs(i, initial);
            if (count < min) {
                min = count;
                node = initial[i];
            }
            count = 0;
            adj.put(initial[i], children);
        }
        return node;
    }
}