Leetcode Problem 2092. Find All People With Secret
2092. Find All People With Secret
Leetcode Solutions
Union Find with Sorting by Meeting Time
Define a Union Find class with methods to find the representative of a set, union two sets, and check if two elements are in the same set.
Initialize the Union Find structure for n people.
Union person 0 with firstPerson as they initially know the secret.
Sort the meetings array based on the meeting times.
Iterate through the sorted meetings, grouping people who meet at the same time and performing unions in the Union Find structure.
After processing all meetings at a particular time, iterate through the people involved in those meetings and reset their Union Find parent if they are not connected to person 0.
After all meetings are processed, iterate through all people and add those connected to person 0 to the result list.