Class Solution
- java.lang.Object
-
- g0901_1000.s0924_minimize_malware_spread.Solution
-
public class Solution extends Object
924 - Minimize Malware Spread.Hard
You are given a network of
n
nodes represented as ann x n
adjacency matrixgraph
, where theith
node is directly connected to thejth
node ifgraph[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 frominitial
.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 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]
is0
or1
.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.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
minMalwareSpread(int[][] graph, int[] initial)
-