1
var findAllPeople = function (n, meetings, firstPerson) {2
const timeToMeeting = mapSortedTimeToMeetings(meetings);4
const peopleThatCurrentlyHaveSecret = new Set([0, firstPerson]);5
for (const peopleInMeetings of timeToMeeting.values()) {6
const personToMeetingsWithPeople =7
mapPeopleToPeopleTheyAreHavingMeetingsWith(peopleInMeetings);8
let peopleInMeetingsWithSecret = findPeopleThatHaveTheSecret(10
peopleThatCurrentlyHaveSecret14
while (peopleInMeetingsWithSecret.size > 0) {15
const nextPeopleInMeetingsWithSecret = new Set();16
for (const attendee of peopleInMeetingsWithSecret) {17
for (const personInMeetingWithAttendee of personToMeetingsWithPeople[20
// only add new people that have the secret otherwise there will be an22
if (!peopleThatCurrentlyHaveSecret.has(personInMeetingWithAttendee)) {23
nextPeopleInMeetingsWithSecret.add(personInMeetingWithAttendee);24
peopleThatCurrentlyHaveSecret.add(personInMeetingWithAttendee);28
peopleInMeetingsWithSecret = nextPeopleInMeetingsWithSecret;31
return [...peopleThatCurrentlyHaveSecret];34
// groups all the meetings by time35
// keys (time) is sorted in ascending order36
function mapSortedTimeToMeetings(meetings) {37
meetings.sort((a, b) => a[2] - b[2]);39
const timeToMeeting = new Map();40
for (const [person1, person2, time] of meetings) {41
if (!timeToMeeting.has(time)) {42
timeToMeeting.set(time, []);44
timeToMeeting.get(time).push([person1, person2]);49
// creates an adjacency list of people and people they are having meetings with50
function mapPeopleToPeopleTheyAreHavingMeetingsWith(peopleInMeetings) {51
const personToMeetingsWithPeople = {};52
for (const [person1, person2] of peopleInMeetings) {53
if (!personToMeetingsWithPeople[person1]) {54
personToMeetingsWithPeople[person1] = [];56
if (!personToMeetingsWithPeople[person2]) {57
personToMeetingsWithPeople[person2] = [];59
personToMeetingsWithPeople[person1].push(person2);60
personToMeetingsWithPeople[person2].push(person1);62
return personToMeetingsWithPeople;65
// finds all the people that are in meetings that have the secret66
// set data structue is used so that people are not duplicated67
function findPeopleThatHaveTheSecret(69
peopleThatCurrentlyHaveSecret71
const peopleInMeetingsWithSecret = new Set();72
for (const peopleInMeeting of peopleInMeetings) {73
for (const person of peopleInMeeting) {74
if (peopleThatCurrentlyHaveSecret.has(person)) {75
peopleInMeetingsWithSecret.add(person);79
return peopleInMeetingsWithSecret;