-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 1520 ms (14.29%) | Memory: 114.8 MB (42.86%) - LeetSync
- Loading branch information
1 parent
966246d
commit 6b09aac
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
2213-find-all-people-with-secret/find-all-people-with-secret.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @param {number} n | ||
* @param {number[][]} meetings | ||
* @param {number} firstPerson | ||
* @return {number[]} | ||
*/ | ||
var findAllPeople = function(n, meetings, firstPerson) { | ||
let knownSet = new Set([0, firstPerson]); | ||
|
||
let sortedMeetings = []; | ||
meetings.sort((a, b) => a[2] - b[2]); | ||
|
||
let seenTime = new Set(); | ||
|
||
for (let meeting of meetings) { | ||
if (!seenTime.has(meeting[2])) { | ||
seenTime.add(meeting[2]); | ||
sortedMeetings.push([]); | ||
} | ||
sortedMeetings[sortedMeetings.length - 1].push([meeting[0], meeting[1]]); | ||
} | ||
|
||
for (let meetingGroup of sortedMeetings) { | ||
let peopleKnowSecret = new Set(); | ||
let graph = {}; | ||
|
||
for (let [p1, p2] of meetingGroup) { | ||
if (!graph[p1]) graph[p1] = []; | ||
if (!graph[p2]) graph[p2] = []; | ||
|
||
graph[p1].push(p2); | ||
graph[p2].push(p1); | ||
|
||
if (knownSet.has(p1)) peopleKnowSecret.add(p1); | ||
if (knownSet.has(p2)) peopleKnowSecret.add(p2); | ||
} | ||
|
||
let queue = [...peopleKnowSecret]; | ||
|
||
while (queue.length > 0) { | ||
let curr = queue.shift(); | ||
knownSet.add(curr); | ||
for (let neigh of graph[curr]) { | ||
if (!knownSet.has(neigh)) { | ||
knownSet.add(neigh); | ||
queue.push(neigh); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return [...knownSet]; | ||
}; |