[Swift]LeetCode924.儘可能減小惡意軟件的傳播 | Minimize Malware Spread

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-dlikzcau-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.node

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

Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.github

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:app

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1] Output: 0 

Example 2:this

Input: graph = [[1,0,0],[0,1,0],[0,0,1]], initial = [0,2] Output: 0 

Example 3:spa

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2] Output: 1 

 Note:code

  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 }
相關文章
相關標籤/搜索