forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEarliestMomentFriends.swift
43 lines (33 loc) · 1.08 KB
/
EarliestMomentFriends.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Question Link: https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/
* Primary idea: Classic Union Find, union every node until the count drops down to 1.
*
* Time Complexity: O(nlogn), Space Complexity: O(n)
*
*/
class EarliestMomentFriends {
func earliestAcq(_ logs: [[Int]], _ n: Int) -> Int {
let logs = logs.sorted { $0[0] < $1[0] }
var roots = Array(0..<n), count = n
for log in logs {
let time = log[0], friend1 = log[1], friend2 = log[2]
let root1 = find(friend1, roots)
let root2 = find(friend2, roots)
if root1 != root2 {
roots[root1] = root2
count -= 1
}
if count == 1 {
return time
}
}
return -1
}
private func find(_ node: Int, _ roots: [Int]) -> Int {
var node = node
while node != roots[node] {
node = roots[node]
}
return node
}
}