Skip to content

Commit 3bbaeb2

Browse files
authored
Create 2092-find-all-people-with-secret.js
1 parent 8c17e67 commit 3bbaeb2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

2092-find-all-people-with-secret.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} meetings
4+
* @param {number} firstPerson
5+
* @return {number[]}
6+
*/
7+
const findAllPeople = function(n, meetings, firstPerson) {
8+
meetings.sort((a, b) => a[2] - b[2])
9+
const shared = new Set([0, firstPerson])
10+
11+
let start = new Set(), links = {}
12+
for(let i = 0, len = meetings.length; i < len; i++) {
13+
const [x,y,t] = meetings[i]
14+
if(i > 0 && t !== meetings[i - 1][2]) {
15+
dfs(start, links, shared)
16+
start = new Set()
17+
links = {}
18+
}
19+
if(shared.has(x)) start.add(x)
20+
if(shared.has(y)) start.add(y)
21+
if(links[x] == null) links[x] = []
22+
if(links[y] == null) links[y] = []
23+
links[x].push(y)
24+
links[y].push(x)
25+
}
26+
27+
28+
dfs(start, links, shared)
29+
return Array.from(shared)
30+
31+
function dfs(start, links, shared) {
32+
const visited = new Set()
33+
while(start.size) {
34+
const it = start[Symbol.iterator]()
35+
const cur = it.next().value
36+
start.delete(cur)
37+
visited.add(cur)
38+
shared.add(cur)
39+
for(let e of (links[cur] || [])) {
40+
if(!visited.has(e)) start.add(e)
41+
}
42+
}
43+
}
44+
};
45+
46+

0 commit comments

Comments
 (0)