-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_2092.kt
67 lines (55 loc) · 1.6 KB
/
LC_2092.kt
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Solution {
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
val secret = mutableSetOf(0, firstPerson)
val noSecret = mutableSetOf<Int>()
val uf = DisjointSetImpl(n)
uf.union(0,firstPerson)
val meetingMap = mutableMapOf<Int,Set<Pair<Int,Int>>>()
meetings.forEach { (x,y,time) ->
meetingMap.merge(time, mutableSetOf(Pair(x,y))) {
v1,_ -> v1.plus(Pair(x,y))
}
}
val keySet = meetingMap.keys.toSortedSet()
keySet.forEach { time->
val curSet = mutableSetOf<Int>()
meetingMap[time]!!.forEach { (x,y)->
uf.union(x,y)
curSet.add(x)
curSet.add(y)
}
curSet.forEach {
if(uf.find(it) == uf.find(0)){
secret.add(it)
} else {
noSecret.add(it)
}
}
}
return secret.toList()
}
}
class DisjointSetImpl(size: Int) : DisjointSet {
private var arr: IntArray
init {
arr = IntArray(size + 1) { it }
}
override fun union(a: Int, b: Int) {
val ap = find(a)
val bp = find(b)
arr[bp] = ap
}
override fun find(target: Int): Int {
val parent = arr[target]
return if (target == parent) {
target
} else {
arr[target] = find(parent)
arr[target]
}
}
}
interface DisjointSet {
fun union(a: Int, b: Int)
fun find(target: Int): Int
}