File tree 1 file changed +42
-0
lines changed
1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## Minimize Malware Spread II
2
+
3
+ #### Description
4
+
5
+ [ link] ( https://leetcode.com/problems/possible-bipartition/ )
6
+
7
+ ---
8
+
9
+ #### Solution
10
+
11
+ - See Code
12
+
13
+ ---
14
+
15
+ #### Code
16
+
17
+ > Complexity T : O(E log E)
18
+
19
+ ``` python
20
+ class Solution :
21
+ def minMalwareSpread (self , graph : List[List[int ]], initial : List[int ]) -> int :
22
+ n = len (graph)
23
+ d = collections.defaultdict(list )
24
+ for init in initial:
25
+ vis = set (initial)
26
+ Q = collections.deque([init])
27
+ while Q:
28
+ infect = Q.popleft()
29
+ for node in range (len (graph[infect])):
30
+ if graph[infect][node] == 0 : continue
31
+ if node in vis: continue
32
+ vis.add(node)
33
+ d[node].append(init)
34
+ Q.append(node)
35
+ # count the most frequent node
36
+ res = [0 ] * n
37
+ for key in d:
38
+ if len (d[key]) == 1 :
39
+ res[d[key][0 ]] += 1
40
+ if max (res) == 0 : return min (initial)
41
+ return res.index(max (res))
42
+ ```
You can’t perform that action at this time.
0 commit comments