Skip to content

Commit e8c19c0

Browse files
authored
Create 2092-find-all-people-with-secret.kt
1 parent fadf450 commit e8c19c0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
3+
val secrets = mutableSetOf(0, firstPerson)
4+
val timeMap = mutableMapOf<Int, MutableMap<Int, MutableList<Int>>>()
5+
6+
for ((src, dst, t) in meetings) {
7+
if (t !in timeMap)
8+
timeMap[t] = mutableMapOf()
9+
timeMap[t]!!.getOrPut(src) { mutableListOf() }.apply { add(dst) }
10+
timeMap[t]!!.getOrPut(dst) { mutableListOf() }.apply { add(src) }
11+
}
12+
13+
fun dfs(
14+
src: Int,
15+
adj: MutableMap<Int, MutableList<Int>>,
16+
visit: MutableSet<Int>
17+
) {
18+
if (src in visit) return
19+
visit.add(src)
20+
secrets.add(src)
21+
adj[src]?.forEach { nei ->
22+
dfs(nei, adj, visit)
23+
}
24+
}
25+
26+
timeMap.keys.sorted().forEach { t ->
27+
val visit = mutableSetOf<Int> ()
28+
timeMap[t]?.keys?.forEach { src ->
29+
if (src in secrets) {
30+
dfs(src, timeMap[t]!!, visit)
31+
}
32+
}
33+
}
34+
35+
return secrets.toList()
36+
}
37+
}

0 commit comments

Comments
 (0)