Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/928.html
928. Minimize Malware Spread II (Hard)
(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 < graph.length = graph[0].length <= 300
0 <= graph[i][j] == graph[j][i] <= 1
graph[i][i] = 1
1 <= initial.length < graph.length
0 <= initial[i] < graph.length
Companies:
Dropbox
Related Topics:
Depth-first Search, Union Find, Graph
Solution 1. Union Find
// OJ: https://leetcode.com/problems/minimize-malware-spread-ii/
// Time: O(N^2)
// Space: O(N)
class UnionFind {
private:
vector<int> rank, id, size;
public:
UnionFind(int n): rank(n), 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 {
private:
int getInfectedCount(vector<vector<int>>& graph, int skip, vector<int>& initial) {
int N = graph.size(), ans = 0;
UnionFind uf(N);
for (int i = 0; i < N; ++i) {
if (i == skip) continue;
for (int j = i + 1; j < N; ++j) {
if (j == skip || !graph[i][j]) continue;
uf.connect(i, j);
}
}
unordered_set<int> s;
for (int n : initial) s.insert(uf.find(n));
for (int n : s) ans += uf.getUnionSize(n);
return ans;
}
public:
int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
int ans = initial[0], minCnt = INT_MAX;
sort(initial.begin(), initial.end());
for (int n : initial) {
int cnt = getInfectedCount(graph, n, initial);
if (cnt < minCnt) {
ans = n;
minCnt = cnt;
}
}
return ans;
}
};
-
class Solution { public int minMalwareSpread(int[][] graph, int[] initial) { Arrays.sort(initial); int length = graph.length; Set<Integer> initialSet = new HashSet<Integer>(); for (int infected : initial) initialSet.add(infected); Map<Integer, Set<Integer>> connectionMap = new HashMap<Integer, Set<Integer>>(); Map<Integer, Set<Integer>> infectedMap = new HashMap<Integer, Set<Integer>>(); for (int infected : initial) { Set<Integer> connectionSet = new HashSet<Integer>(); boolean[] visited = new boolean[length]; visited[infected] = true; Queue<Integer> queue = new LinkedList<Integer>(); queue.offer(infected); while (!queue.isEmpty()) { int node = queue.poll(); for (int i = 0; i < length; i++) { if (graph[node][i] == 1 && !visited[i] && !initialSet.contains(i)) { visited[i] = true; queue.offer(i); connectionSet.add(i); Set<Integer> set = infectedMap.getOrDefault(i, new HashSet<Integer>()); set.add(infected); infectedMap.put(i, set); } } } connectionMap.put(infected, connectionSet); } int remove = -1; int maxDecrease = 0; for (int infected : initial) { int decrease = 0; Set<Integer> connectionSet = connectionMap.getOrDefault(infected, new HashSet<Integer>()); for (int node : connectionSet) { Set<Integer> infectedSet = infectedMap.get(node); if (infectedSet.size() == 1) decrease++; } if (remove == -1 || decrease > maxDecrease) { remove = infected; maxDecrease = decrease; } } return remove; } } ############ class Solution { private int[] p; private int[] size; public int minMalwareSpread(int[][] graph, int[] initial) { int n = graph.length; p = new int[n]; size = new int[n]; for (int i = 0; i < n; ++i) { p[i] = i; size[i] = 1; } boolean[] clean = new boolean[n]; Arrays.fill(clean, true); for (int i : initial) { clean[i] = false; } for (int i = 0; i < n; ++i) { if (!clean[i]) { continue; } for (int j = i + 1; j < n; ++j) { if (clean[j] && graph[i][j] == 1) { union(i, j); } } } int[] cnt = new int[n]; Map<Integer, Set<Integer>> mp = new HashMap<>(); for (int i : initial) { Set<Integer> s = new HashSet<>(); for (int j = 0; j < n; ++j) { if (clean[j] && graph[i][j] == 1) { s.add(find(j)); } } for (int root : s) { cnt[root] += 1; } mp.put(i, s); } int mx = -1; int ans = 0; for (Map.Entry<Integer, Set<Integer>> entry : mp.entrySet()) { int i = entry.getKey(); int t = 0; for (int root : entry.getValue()) { if (cnt[root] == 1) { t += size[root]; } } if (mx < t || (mx == t && i < ans)) { mx = t; ans = i; } } return ans; } private int find(int x) { if (p[x] != x) { p[x] = find(p[x]); } return p[x]; } private void union(int a, int b) { int pa = find(a); int pb = find(b); if (pa != pb) { size[pb] += size[pa]; p[pa] = pb; } } }
-
// OJ: https://leetcode.com/problems/minimize-malware-spread-ii/ // Time: O(N^2) // Space: O(N) class UnionFind { private: vector<int> rank, id, size; public: UnionFind(int n): rank(n), 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 { private: int getInfectedCount(vector<vector<int>>& graph, int skip, vector<int>& initial) { int N = graph.size(), ans = 0; UnionFind uf(N); for (int i = 0; i < N; ++i) { if (i == skip) continue; for (int j = i + 1; j < N; ++j) { if (j == skip || !graph[i][j]) continue; uf.connect(i, j); } } unordered_set<int> s; for (int n : initial) s.insert(uf.find(n)); for (int n : s) ans += uf.getUnionSize(n); return ans; } public: int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) { int ans = initial[0], minCnt = INT_MAX; sort(initial.begin(), initial.end()); for (int n : initial) { int cnt = getInfectedCount(graph, n, initial); if (cnt < minCnt) { ans = n; minCnt = cnt; } } return ans; } };
-
class Solution: def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int: def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] def union(a, b): pa, pb = find(a), find(b) if pa != pb: size[pb] += size[pa] p[pa] = pb n = len(graph) p = list(range(n)) size = [1] * n clean = [True] * n for i in initial: clean[i] = False for i in range(n): if not clean[i]: continue for j in range(i + 1, n): if clean[j] and graph[i][j] == 1: union(i, j) cnt = Counter() mp = {} for i in initial: s = {find(j) for j in range(n) if clean[j] and graph[i][j] == 1} for root in s: cnt[root] += 1 mp[i] = s mx, ans = -1, 0 for i, s in mp.items(): t = sum(size[root] for root in s if cnt[root] == 1) if mx < t or mx == t and i < ans: mx, ans = t, i return ans
-
func minMalwareSpread(graph [][]int, initial []int) int { n := len(graph) p := make([]int, n) size := make([]int, n) clean := make([]bool, n) for i := 0; i < n; i++ { p[i], size[i], clean[i] = i, 1, true } for _, i := range initial { clean[i] = false } var find func(x int) int find = func(x int) int { if p[x] != x { p[x] = find(p[x]) } return p[x] } union := func(a, b int) { pa, pb := find(a), find(b) if pa != pb { size[pb] += size[pa] p[pa] = pb } } for i := 0; i < n; i++ { if !clean[i] { continue } for j := i + 1; j < n; j++ { if clean[j] && graph[i][j] == 1 { union(i, j) } } } cnt := make([]int, n) mp := make(map[int]map[int]bool) for _, i := range initial { s := make(map[int]bool) for j := 0; j < n; j++ { if clean[j] && graph[i][j] == 1 { s[find(j)] = true } } for root, _ := range s { cnt[root]++ } mp[i] = s } mx, ans := -1, 0 for i, s := range mp { t := 0 for root, _ := range s { if cnt[root] == 1 { t += size[root] } } if mx < t || (mx == t && i < ans) { mx, ans = t, i } } return ans }