[Swift]LeetCode924.尽量减少恶意软件的传播 | Minimize Malware Spread

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9832226.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.  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.

Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of 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

 Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

在节点网络中,只有当 graph[i][j] = 1 时,每个节点 i 能够直接连接到另一个节点 j

一些节点 initial 最初被恶意软件感染。只要两个节点直接连接,且其中至少一个节点受到恶意软件的感染,那么两个节点都将被恶意软件感染。这种恶意软件的传播将继续,直到没有更多的节点可以被这种方式感染。

假设 M(initial) 是在恶意软件停止传播之后,整个网络中感染恶意软件的最终节点数。

我们可以从初始列表中删除一个节点。如果移除这一节点将最小化 M(initial), 则返回该节点。如果有多个节点满足条件,就返回索引最小的节点。

请注意,如果某个节点已从受感染节点的列表 initial 中删除,它以后可能仍然因恶意软件传播而受到感染。

 示例 1:

输入:graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
输出:0

示例 2:

输入:graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2]
输出:0

示例 3:

输入:graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
输出:1

 提示:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

2592ms
 1 class Solution {
 2     func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
 3         //1.为每个组件着色。
 4         //colors[node]为n此ode节点的颜色
 5         let N:Int = graph.count
 6         var colors:[Int] = [Int](repeating: -1,count: N) 
 7         var C:Int = 0
 8         
 9         for node in 0..<N
10         {
11             if colors[node] == -1
12             {
13                 dfs(graph, &colors, node, C)
14                 C += 1
15             }
16         }
17         
18         //2.每种颜色的大小
19         var size:[Int] = [Int](repeating: 0,count: C) 
20         for color in colors
21         {
22             size[color] += 1
23         }
24         
25         //3.找到独特的颜色
26         var colorCount:[Int] = [Int](repeating: 0,count: C) 
27         for node in initial
28         {
29             colorCount[colors[node]] += 1
30         }
31         
32         //4.答案
33         var ans:Int = Int.max
34         for node in initial
35         {
36             var c:Int = colors[node]
37             if colorCount[c] == 1
38             {
39                 if ans == Int.max
40                 {
41                     ans = node
42                 }
43                 else if size[c] > size[colors[ans]]
44                 {
45                     ans = node
46                 }
47                 else if size[c] == size[colors[ans]] && node < ans
48                 {
49                     ans = node
50                 }   
51             }
52         }
53         
54         if ans == Int.max
55         {
56             for node in initial
57             {
58                 ans = min(ans,node)
59             }
60         } 
61         
62         return ans
63     }
64     
65     func dfs(_ graph:[[Int]], _ colors:inout [Int],_ node:Int,_ color:Int)
66     {
67         colors[node] = color
68         for nei in 0..<graph.count
69         {
70             if graph[node][nei] == 1 && colors[nei] == -1
71             {
72                 dfs(graph, &colors, nei, color)
73             }
74         }
75     }  
76 }

2708ms
 1 class Solution {
 2     func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
 3         struct UnionFind {
 4             private var parent = [Int]()
 5             private var size = [Int]()
 6             private let initial: Set<Int>
 7 
 8             public init(count: Int, initial: [Int]) {
 9                 for i in 0..<count {
10                     parent.append(i)
11                     size.append(1)
12                 }
13                 self.initial = Set(initial)
14             }
15 
16             public mutating func find(_ element: Int) -> Int {
17                 if element == parent[element] {
18                     return element
19                 } else {
20                     parent[element] = parent[parent[element]]
21                     return parent[element]
22                 }
23             }
24 
25             public mutating func union(_ lhs: Int, _ rhs: Int) {
26                 let parentOfLhs = find(lhs)
27                 let parentOfRhs = find(rhs)
28                 if (size[parentOfLhs] > size[parentOfRhs]) {
29                     parent[parentOfRhs] = parentOfLhs
30                     size[parentOfLhs] += size[parentOfRhs]
31                     size[parentOfRhs] = 0
32                 } else {
33                     parent[parentOfLhs] = parentOfRhs
34                     size[parentOfRhs] += size[parentOfLhs]
35                     size[parentOfLhs] = 0
36                 }
37             }
38 
39             public func max() -> (index: Int, count: Int) {
40                 var result = (index: size.count, count: -1)
41                 for i in initial {
42                     var count = size[parent[i]]
43                     for j in initial {
44                         if i != j && parent[i] == parent[j] {
45                             count = 0
46                             break
47                         }
48                     }
49                     if count > result.count || count == result.count && i < result.index {
50                         result = (i, count)
51                     }
52                 }
53                 return result
54             }
55         }
56 
57         // If # of columns != # of rows
58         guard graph.count == graph.first?.count else {
59             return 0
60         }
61 
62         let n = graph.count
63         var unionFind = UnionFind(count: n, initial: initial)
64         for i in 0..<n - 1 {
65             for j in i + 1..<n {
66                 if graph[i][j] == 1 {
67                     unionFind.union(i, j)
68                 }
69             }
70         }
71 
72         let result = unionFind.max()
73         return result.0
74     }
75 }

3424ms

 1 class Solution {
 2   var visited: Set<Int> = Set()
 3   var initial: Set<Int> = Set() 
 4   var graph : [[Int]] = []
 5   
 6   func traverse(_ pos: Int) -> Int {
 7     var result = 1
 8     visited.insert(pos)
 9     for i in 0..<graph.count {
10       if i != pos && graph[pos][i] == 1 {
11         if !visited.contains(i) {
12           result += traverse(i)
13         }
14       }
15     }
16     return result
17   }
18   
19   func countComponent(_ start: Int) -> Int {
20     return traverse(start)
21   }
22   
23   func minMalwareSpread(_ graph: [[Int]], _ initial: [Int]) -> Int {
24     self.graph = graph
25     self.initial = Set<Int>(initial)
26     
27     var maxcnt = 0
28     var maxi = -1
29     
30     for x in initial {
31       visited = Set()
32       let n = countComponent(x)
33       if (n > maxcnt || (n==maxcnt && x < maxi)) && visited.intersection(self.initial).count == 1 {
34         maxcnt = n
35         maxi = x
36       }
37     }
38     if maxi == -1 {return self.initial.min()! }
39     return maxi
40   }
41   
42 }
原文地址:https://www.cnblogs.com/strengthen/p/9832226.html