924 Minimize Malware Spread
1. Question
In a network of nodes, each nodei
is directly connected to another nodej
if and only if graph[i][j] = 1
.
Some nodesinitial
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.
SupposeM(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. Return the node that if removed, would minimize M(initial)
. If multiple nodes could be removed to minimizeM(initial)
, return such a node with the smallest index.
Note that if a node was removed from theinitial
list of infected nodes, it may still be infected later as a result of the malware spread.
Example 1:
Example 2:
Example 3:
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
2. Implementation
(1) Union Find:
思路: 这题要求我们在输入的initial list里移除一个node,使得最后感染的node的数量最少。注意这里移除的意思是指,让initial list中原本被感染的node变成不被感染,而不是说将这个node从图中删除, 这点是和leetcode 928的区别。
利用union find,构建并查集
查找在initial list里的node属于哪个并查集,用hashmap记录initial list每个node所属的并查集,以及其所对应的个数。如果一个并查集里含有两个或以上的node被感染,则根据题意只能让一个node不感染,那这个并查集不管移除哪个node都不能避免集合里的node不被感染。如果一个集合里只有一个node被感染,则找出拥有node个数最多的这类集合。
由于题目要求,如果有多个node都能使被感染的node数量最小时,返回index最小的那个,所以我们需要将initial list排序
3. Time & Space Complexity
时间复杂度O(n^2), 主要消耗是在构建union find这一步,空间复杂度O(n)
Last updated
Was this helpful?