[Swift]LeetCode685. 冗余连接 II | Redundant Connection II

[Swift]LeetCode685. 冗余连接 II | Redundant Connection II,第1张

概述In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, excep

In this problem,a rooted tree is a directed graph such that,there is exactly one node (the root) for which all other nodes are descendants of this node,plus every node has exactly one parent,except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1,2,...,N),with one additional directed edge added. The added edge has two different vertices chosen from 1 to N,and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u,v] that represents a directed edge connecting nodes u and v,where u is a parent of child v.

Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers,return the answer that occurs last in the given 2D-array.

Example 1:

input: [[1,2],[1,3],[2,3]]Output: [2,3]Explanation: The given directed graph will be like this:  1 / v   v2-->3 

Example 2:

input: [[1,[3,4],[4,1],5]]Output: [4,1]Explanation: The given directed graph will be like this:5 <- 1 -> 2     ^    |     |    v     4 <- 3 

Note:

The size of the input 2D-array will be between 3 and 1000. Every integer represented in the 2D-array will be between 1 and N,where N is the size of the input array.

在本问题中,有根树指满足以下条件的有向图。该树只有一个根节点,所有其他节点都是该根节点的后继。每一个节点只有一个父节点,除了根节点没有父节点。

输入一个有向图,该图由一个有着N个节点 (节点值不重复1,N) 的树及一条附加的边构成。附加的边的两个顶点包含在1到N中间,这条附加的边不属于树中已存在的边。

结果图是一个以组成的二维数组。 每一个 的元素是一对 [u,v],用以表示有向图中连接顶点 u and v和顶点的边,其中父节点u是子节点v的一个父节点。

返回一条能删除的边,使得剩下的图是有N个节点的有根树。若有多个答案,返回最后出现在给定二维数组的答案。

示例 1:

输入: [[1,3]]输出: [2,3]解释: 给定的有向图如下:  1 / v   v2-->3

示例 2:

输入: [[1,5]]输出: [4,1]解释: 给定的有向图如下:5 <- 1 -> 2     ^    |     |    v     4 <- 3

注意:

二维数组大小的在3到1000范围内。 二维数组中的每个整数在1到N之间,其中 N 是二维数组的大小。

36ms

 1 class Solution { 2     func findRedundantDirectedConnection(_ edges: [[Int]]) -> [Int] { 3         guard edges.isEmpty == false else { 4             return [] 5         } 6         var parents: [Int] = [] 7          8         var res1: [Int] = [] 9         var res2: [Int] = []10         11         for i in 0...edges.count {12             parents.append(i)13         }14         for edge in edges {15             let x = edge[0]16             let y = edge[1]17             let rootX = find(&parents,x)18             let rootY = find(&parents,y)19             if rootX != rootY {20                 if rootY != y {21                     res1 = edge22                 } else {23                     parents[y] = x24                 }25             } else {26                 res2 = edge27             }28         }29         if res1.isEmpty == true {30             return res231         }32         if res2.isEmpty == true {33             return res134         }35         36         for edge in edges {37             if edge[1] == res1[1] {38                 return edge39             }40         }41         return []42     }43     44     private func find(_ parents: inout [Int],_ val: Int) -> Int {45         if parents[val] != val {46             parents[val] = find(&parents,parents[val])47         }48         return parents[val]49     }50 }

40ms

 1 class Solution { 2     func findRedundantDirectedConnection(_ edges: [[Int]]) -> [Int] { 3         guard edges.isEmpty == false else { 4             return [] 5         } 6         var parents: [Int] = [] 7          8         var res1: [Int] = [] 9         var res2: [Int] = []10         11         for i in 0...edges.count {12             parents.append(i)13         }14         for edge in edges {15             let x = edge[0]16             let y = edge[1]17             let rootX = find(parents,x)18             let rootY = find(parents,y)19             if rootX != rootY {20                 if rootY != y {21                     res1 = edge22                 } else {23                     parents[y] = x24                 }25             } else {26                 res2 = edge27             }28         }29         if res1.isEmpty == true {30             return res231         }32         if res2.isEmpty == true {33             return res134         }35         36         for edge in edges {37             if edge[1] == res1[1] {38                 return edge39             }40         }41         return []42     }43     44     private func find(_ parents: [Int],_ val: Int) -> Int {45         if val == parents[val] {46             return val47         }48         return find(parents,parents[val])49     }50 }
Runtime: 44 ms Memory Usage: 19.1 MB
 1 class Solution { 2     func findRedundantDirectedConnection(_ edges: [[Int]]) -> [Int] { 3         var edges = edges 4         var n:Int = edges.count 5         var root:[Int] = [Int](repeating:0,count:n + 1) 6         var first:[Int] = [Int]() 7         var second:[Int] = [Int]() 8         for i in 0..<edges.count 9         {10             if root[edges[i][1]] == 011             {12                 root[edges[i][1]] = edges[i][0]13             }14             else15             {16                 first = [root[edges[i][1]],edges[i][1]]17                 second = edges[i]18                 edges[i][1] = 019             }20         }21         for i in 0...n22         {23             root[i] = i24         }25         for edge in edges26         {27             if edge[1] == 0 {continue}28             var x:Int = getRoot(&root,edge[0])29             var y:Int = getRoot(&root,edge[1])30             if x == y31             {32                 return first.isEmpty ? edge : first33             }34             root[x] = y            35         }36         return second37     }38     39     func getRoot(_ root:inout [Int],_ i:Int) -> Int40     {41         return i == root[i] ? i : getRoot(&root,root[i])42     }43 }

52ms

 1 class Solution { 2     var parents = [Int: Int]() 3     var candIDate1 = [-1,-1] 4     var candIDate2 = [-1,-1] 5      6     func findRedundantDirectedConnection(_ edges: [[Int]]) -> [Int] { 7         var edges = edges 8         // ****** Step 1: Check if there is a 2-parent situation. ****** 9         for i in 0..<edges.count {10             let parent = edges[i][0]11             let child = edges[i][1]12             if let existedParent = parents[child] {13                   // If there is already an existed parent for the current child,this means we have found a 2-parent situation.14                 // Set candIDate1 to the prevIoUsly-found edge.15                 candIDate1 = [existedParent,child]16                 // Set candIDate2 to the current edge.17                 candIDate2 = edges[i]18                 // InvalIDate the curent edge.19                 edges[i][1] = -120             } else {21                 // If there is no 2-parent situation,we record the parent for each child.22                 parents[child] = parent23             }24         }25         26         // ****** Step2: Perform union-find to find if there is any cycles. ******27         // reset parent dictionary.28         parents = [Int: Int]()29         // Perform union-find.30         var foundEdge: [Int]?31         for edge in edges {32             guard edge[1] != -1 else {33                 continue34             }35             if let foundEdge = union(edge[0],edge[1]) {36                 return foundEdge37             }38         }39         // If we reach this point,that means we prevIoUsly invalIDated the corrent "wrong" edge and there is no cycle,40         // which is situation #1,just return candIDate2.41         return candIDate242     }43     44     private func union(_ x: Int,_ y: Int) -> [Int]? {45         let xParnet = find(x)46         let yParent = find(y)47         guard xParnet != yParent else {48             // If we have found a cycle...49             if candIDate1[0] == -1 {50                 // If there is no 2-parent situation at all,which is situation #2,return the current edge.51                 return [x,y]52             } else {53                  // If there is a 2-parent situation,which is situation #3,return candIDate 1.54                 return candIDate155             }56         }57         // If no cycle is found,we are good and return nothing.58         parents[yParent] = xParnet59         return nil60     }61     62     private func find(_ x: Int) -> Int {63         // Recursively find the parent or return the node itself if this node has no parent.64         if parents[x] == nil {65             return x66         }67         return find(parents[x]!)68     }69 }

56ms

 1 class Solution { 2     func findRedundantDirectedConnection(_ edges: [[Int]]) -> [Int] { 3       var adjacentInfo: [Int: Set<Int>] = [:] 4       var parentInfo: [Int: [Int]] = [:] 5       var target: Int = -1 6       for edge in edges { 7         var adjacentSet: Set<Int> 8         if let adJset = adjacentInfo[edge[0]] { 9           adjacentSet = adJset10         } else {11           adjacentSet = Set()12         }13         adjacentSet.insert(edge[1])14         adjacentInfo[edge[0]] = adjacentSet15         16         if var parents = parentInfo[edge[1]] {17           target = edge[1]18           parents.append(edge[0])19           parentInfo[edge[1]] = parents20         } else{21           parentInfo[edge[1]] = [edge[0]]22         }23       }24       25       if target != -1,let parents = parentInfo[target] {26         var root: Int = parents.first!27         for key in adjacentInfo.keys {28           if parentInfo[key] == nil {29             root = key30             break31           }32         }33         34         let candIDate = parents.last!35         36         var removed = adjacentInfo[candIDate]37         removed?.remove(target)38         adjacentInfo[candIDate] = removed ?? []39         if hasPath(from: root,to: target,info: adjacentInfo) {40           return [candIDate,target]41         } else {42           return [parents.first!,target]43         }44       }45       46       for edge in edges.reversed() {47         if hasPath(from: edge[1],to: edge[0],info: adjacentInfo) {48           return edge49         }50       }51       return []52     }53     func hasPath(from: Int,to: Int,info: [Int: Set<Int>]) -> Bool {54       var visited: Set<Int> = Set()55       var queue: [Int] = [from]56       while queue.count > 0 {57         let current = queue.first!58         queue.remove(at: 0)59         visited.insert(current)60         61         if current == to {62           return true63         }64         guard let adJs = info[current] else { continue }65         for neighbor in adJs {66           if visited.contains(neighbor) == false {67             queue.append(neighbor)68           }69         }70       }71       return false72     }73 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode685. 冗余连接 II | Redundant Connection II全部内容,希望文章能够帮你解决[Swift]LeetCode685. 冗余连接 II | Redundant Connection II所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1019513.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存