在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a social group, there are We have a list of Each log represents the time in which two different people became friends. Friendship is symmetric: if A is friends with B, then B is friends with A. Let's say that person A is acquainted with person B if A is friends with B, or A is a friend of someone acquainted with B. Return the earliest time for which every person became acquainted with every other person. Return -1 if there is no such earliest time. Example 1: Input: logs = 6
Output: 20190301
Explanation:
The first event occurs at timestamp = 20190101 and after 0 and 1 become friends we have the following friendship groups [0,1], [2], [3], [4], [5].
The second event occurs at timestamp = 20190104 and after 3 and 4 become friends we have the following friendship groups [0,1], [2], [3,4], [5].
The third event occurs at timestamp = 20190107 and after 2 and 3 become friends we have the following friendship groups [0,1], [2,3,4], [5].
The fourth event occurs at timestamp = 20190211 and after 1 and 5 become friends we have the following friendship groups [0,1,5], [2,3,4].
The fifth event occurs at timestamp = 20190224 and as 2 and 4 are already friend anything happens.
The sixth event occurs at timestamp = 20190301 and after 0 and 3 become friends we have that all become friends.
Note:
在一个社交圈子当中,有 我们有一份日志列表 每条日志标识出两个人成为好友的时间,友谊是相互的:如果 A 和 B 是好友,那么 B 和 A 也是好友。 如果 A 是 B 的好友,或者 A 是 B 的好友的好友,那么就可以认为 A 也与 B 熟识。 返回圈子里所有人之间都熟识的最早时间。如果找不到最早时间,就返回 示例: 输入:logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], N = 6 输出:20190301 解释: 第一次结交发生在 timestamp = 20190101,0 和 1 成为好友,社交朋友圈如下 [0,1], [2], [3], [4], [5]。 第二次结交发生在 timestamp = 20190104,3 和 4 成为好友,社交朋友圈如下 [0,1], [2], [3,4], [5]. 第三次结交发生在 timestamp = 20190107,2 和 3 成为好友,社交朋友圈如下 [0,1], [2,3,4], [5]. 第四次结交发生在 timestamp = 20190211,1 和 5 成为好友,社交朋友圈如下 [0,1,5], [2,3,4]. 第五次结交发生在 timestamp = 20190224,2 和 4 已经是好友了。 第六次结交发生在 timestamp = 20190301,0 和 3 成为好友,大家都互相熟识了。 提示:
368ms 1 class Solution { 2 func earliestAcq(_ logs: [[Int]], _ N: Int) -> Int { 3 var logs = logs 4 logs.sort(by:{return $0[0] < $1[0]}) 5 let dsu:DisjointSetUnion = DisjointSetUnion(N) 6 for i in logs 7 { 8 dsu.AddEdge(i[1] + 1, i[2] + 1) 9 if dsu.GetSize(1) == N 10 { 11 return i[0] 12 } 13 } 14 return -1 15 } 16 } 17 18 class DisjointSetUnion 19 { 20 var N_:Int 21 var par_:[Int] 22 var size_:[Int] 23 24 init(_ N:Int) 25 { 26 self.N_ = N 27 self.par_ = [Int](repeating:0,count:N_ + 1) 28 self.size_ = [Int](repeating:1,count:N_ + 1) 29 for i in 0..<N 30 { 31 par_[i] = i 32 } 33 } 34 35 // Returns the parent of |x|'s set. 36 func GetParent(_ x:Int) -> Int 37 { 38 if par_[x] != x 39 { 40 par_[x] = GetParent(par_[x]) 41 } 42 return par_[x] 43 } 44 45 // Returns the size of set in which |x| belongs. 46 func GetSize(_ x:Int) -> Int 47 { 48 return size_[GetParent(x)] 49 } 50 51 // Add an edge between |u| and |v|. Creates union of both sets. 52 func AddEdge(_ u:Int,_ v:Int) 53 { 54 var parent_u:Int = GetParent(u) 55 var parent_v:Int = GetParent(v) 56 if parent_u != parent_v 57 { 58 if size_[parent_v] > size_[parent_u] 59 { 60 let temp:Int = parent_u 61 parent_u = parent_v 62 parent_v = temp 63 } 64 par_[parent_v] = parent_u 65 size_[parent_u] += size_[parent_v] 66 size_[parent_v] = 0 67 } 68 } 69 }
|
请发表评论