Welcome to Subscribe On Youtube

924. Minimize Malware Spread

Description

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:

  • 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.

Solutions

  • class Solution {
        private int[] p;
    
        public int minMalwareSpread(int[][] graph, int[] initial) {
            int n = graph.length;
            p = new int[n];
            for (int i = 0; i < n; ++i) {
                p[i] = i;
            }
            int[] size = new int[n];
            Arrays.fill(size, 1);
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (graph[i][j] == 1) {
                        int pa = find(i), pb = find(j);
                        if (pa == pb) {
                            continue;
                        }
                        p[pa] = pb;
                        size[pb] += size[pa];
                    }
                }
            }
            int mi = Integer.MAX_VALUE;
            int res = initial[0];
            Arrays.sort(initial);
            for (int i = 0; i < initial.length; ++i) {
                int t = 0;
                Set<Integer> s = new HashSet<>();
                for (int j = 0; j < initial.length; ++j) {
                    if (i == j) {
                        continue;
                    }
                    if (s.contains(find(initial[j]))) {
                        continue;
                    }
                    s.add(find(initial[j]));
                    t += size[find(initial[j])];
                }
                if (mi > t) {
                    mi = t;
                    res = initial[i];
                }
            }
            return res;
        }
    
        private int find(int x) {
            if (p[x] != x) {
                p[x] = find(p[x]);
            }
            return p[x];
        }
    }
    
  • class Solution {
    public:
        vector<int> p;
    
        int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
            int n = graph.size();
            p.resize(n);
            for (int i = 0; i < n; ++i) p[i] = i;
            vector<int> size(n, 1);
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (graph[i][j]) {
                        int pa = find(i), pb = find(j);
                        if (pa == pb) continue;
                        p[pa] = pb;
                        size[pb] += size[pa];
                    }
                }
            }
            int mi = 400;
            int res = initial[0];
            sort(initial.begin(), initial.end());
            for (int i = 0; i < initial.size(); ++i) {
                int t = 0;
                unordered_set<int> s;
                for (int j = 0; j < initial.size(); ++j) {
                    if (i == j) continue;
                    if (s.count(find(initial[j]))) continue;
                    s.insert(find(initial[j]));
                    t += size[find(initial[j])];
                }
                if (mi > t) {
                    mi = t;
                    res = initial[i];
                }
            }
            return res;
        }
    
        int find(int x) {
            if (p[x] != x) p[x] = find(p[x]);
            return p[x];
        }
    };
    
  • class Solution:
        def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
            n = len(graph)
            p = list(range(n))
            size = [1] * n
    
            def find(x):
                if p[x] != x:
                    p[x] = find(p[x])
                return p[x]
    
            for i in range(n):
                for j in range(i + 1, n):
                    if graph[i][j] == 1:
                        pa, pb = find(i), find(j)
                        if pa == pb:
                            continue
                        p[pa] = pb
                        size[pb] += size[pa]
    
            mi = inf
            res = initial[0]
            initial.sort()
            for i in range(len(initial)):
                t = 0
                s = set()
                for j in range(len(initial)):
                    if i == j:
                        continue
                    if find(initial[j]) in s:
                        continue
                    s.add(find(initial[j]))
                    t += size[find(initial[j])]
                if mi > t:
                    mi = t
                    res = initial[i]
            return res
    
    
  • var p []int
    
    func minMalwareSpread(graph [][]int, initial []int) int {
    	n := len(graph)
    	p = make([]int, n)
    	size := make([]int, n)
    	for i := 0; i < n; i++ {
    		p[i] = i
    		size[i] = 1
    	}
    	for i := 0; i < n; i++ {
    		for j := i + 1; j < n; j++ {
    			if graph[i][j] == 1 {
    				pa, pb := find(i), find(j)
    				if pa == pb {
    					continue
    				}
    				p[pa] = pb
    				size[pb] += size[pa]
    			}
    		}
    	}
    	mi := 400
    	res := initial[0]
    	sort.Ints(initial)
    	for i := 0; i < len(initial); i++ {
    		t := 0
    		s := make(map[int]bool)
    		for j := 0; j < len(initial); j++ {
    			if i == j {
    				continue
    			}
    			if s[find(initial[j])] {
    				continue
    			}
    			s[find(initial[j])] = true
    			t += size[find(initial[j])]
    		}
    		if mi > t {
    			mi = t
    			res = initial[i]
    		}
    	}
    	return res
    }
    
    func find(x int) int {
    	if p[x] != x {
    		p[x] = find(p[x])
    	}
    	return p[x]
    }
    
  • class UnionFind {
        p: number[];
        size: number[];
        constructor(n: number) {
            this.p = Array(n)
                .fill(0)
                .map((_, i) => i);
            this.size = Array(n).fill(1);
        }
    
        find(x: number): number {
            if (this.p[x] !== x) {
                this.p[x] = this.find(this.p[x]);
            }
            return this.p[x];
        }
    
        union(a: number, b: number): boolean {
            const [pa, pb] = [this.find(a), this.find(b)];
            if (pa === pb) {
                return false;
            }
            if (this.size[pa] > this.size[pb]) {
                this.p[pb] = pa;
                this.size[pa] += this.size[pb];
            } else {
                this.p[pa] = pb;
                this.size[pb] += this.size[pa];
            }
            return true;
        }
    
        getSize(root: number): number {
            return this.size[root];
        }
    }
    
    function minMalwareSpread(graph: number[][], initial: number[]): number {
        const n = graph.length;
        const uf = new UnionFind(n);
        for (let i = 0; i < n; ++i) {
            for (let j = i + 1; j < n; ++j) {
                graph[i][j] && uf.union(i, j);
            }
        }
        let [ans, mx] = [n, 0];
        const cnt: number[] = Array(n).fill(0);
        for (const x of initial) {
            ++cnt[uf.find(x)];
        }
        for (const x of initial) {
            const root = uf.find(x);
            if (cnt[root] === 1) {
                const sz = uf.getSize(root);
                if (sz > mx || (sz === mx && x < ans)) {
                    [ans, mx] = [x, sz];
                }
            }
        }
        return ans === n ? Math.min(...initial) : ans;
    }
    
    

All Problems

All Solutions