在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a network of nodes, each node Some nodes Suppose We will remove one node from the initial list. Return the node that if removed, would minimize Note that if a node was removed from the Example 1: Input: graph = [0,1]
Output: 0
Example 2: Input: graph = [0,2]
Output: 0
Example 3: Input: graph = [1,2]
Output: 1
Note:
在节点网络中,只有当 一些节点 假设 我们可以从初始列表中删除一个节点。如果移除这一节点将最小化 请注意,如果某个节点已从受感染节点的列表 示例 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 提示:
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 }
|
请发表评论