LeetCode 928. Minimize Malware Spread II
原题链接在这里:https://leetcode.com/problems/minimize-malware-spread-ii/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
, 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]
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.
题解:
If we want to remove one initial node to minimize the final infected sum, we need to find out how many nodes are only infected by this inital node, but not other initial node.
To do this, we perform BFS from this init node, if we see other init node, we stop.
Matintain a map of node and which init infected this node.
For all the node that has only one init infect node, we need them and calculate which init node infected the most.
Time Complexity: O(m * n ^ 2). n = graph.length. BFS takes O(n^2). m = initial.length.
Space: O(n).
AC Java:
1 class Solution { 2 public int minMalwareSpread(int[][] graph, int[] initial) { 3 int n = graph.length; 4 Map<Integer, List<Integer>> hm = new HashMap<>(); 5 6 for(int init : initial){ 7 Set<Integer> visited = new HashSet<>(); 8 for(int val : initial){ 9 visited.add(val); 10 } 11 12 LinkedList<Integer> que = new LinkedList<>(); 13 que.add(init); 14 15 while(!que.isEmpty()){ 16 int cur = que.poll(); 17 for(int i = 0; i < graph[cur].length; i++){ 18 if(graph[cur][i] == 0 || visited.contains(i)){ 19 continue; 20 } 21 22 visited.add(i); 23 que.add(i); 24 hm.putIfAbsent(i, new ArrayList<Integer>()); 25 hm.get(i).add(init); 26 } 27 } 28 } 29 30 int[] arr = new int[n]; 31 for(int key : hm.keySet()){ 32 if(hm.get(key).size() == 1){ 33 arr[hm.get(key).get(0)]++; 34 } 35 } 36 37 int maxCount = 0; 38 int res = Integer.MAX_VALUE; 39 for(int i : initial){ 40 if(arr[i] > maxCount){ 41 maxCount = arr[i]; 42 res = i; 43 }else if(arr[i] == maxCount && i < res){ 44 res = i; 45 } 46 } 47 48 return res == Integer.MAX_VALUE ? initial[0] : res; 49 } 50 }