All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0901_1000.s0928_minimize_malware_spread_ii.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0901_1000.s0928_minimize_malware_spread_ii;

// #Hard #Array #Depth_First_Search #Breadth_First_Search #Matrix #Union_Find
// #2022_03_29_Time_41_ms_(31.01%)_Space_87.5_MB_(6.33%)

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;

/**
 * 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:**
 *
 * *   `n == graph.length`
 * *   `n == graph[i].length`
 * *   `2 <= n <= 300`
 * *   `graph[i][j]` is `0` or `1`.
 * *   `graph[i][j] == graph[j][i]`
 * *   `graph[i][i] == 1`
 * *   `1 <= initial.length < n`
 * *   `0 <= initial[i] <= n - 1`
 * *   All the integers in `initial` are **unique**.
**/
public class Solution {
    private final Map> adj = new HashMap<>();
    private Set visited;
    private int count = 0;

    private void bfs(int ind, int[] initial) {
        Queue 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 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 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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy