Skip to content

Commit

Permalink
Time: 1520 ms (14.29%) | Memory: 114.8 MB (42.86%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ShatilKhan committed Feb 24, 2024
1 parent 966246d commit 6b09aac
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2213-find-all-people-with-secret/find-all-people-with-secret.js
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];
};

0 comments on commit 6b09aac

Please sign in to comment.