You are given an integer n indicating there are n people numbered from 0
to n - 1. You are also given a 0-indexed 2D integer array meetings
where meetings[i] = [xi, yi, timei] indicates that person xi and person
yi have a meeting at timei. A person may attend multiple meetings at the same time. Finally, you are given an integer firstPerson.
Person 0 has a secret and initially shares the secret with a person
firstPerson at time 0. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person xi has the secret at timei, then they will share the secret with person yi, and vice versa.
The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order.
Input: n =6, meetings =[[1,2,5],[2,3,8],[1,5,10]], firstPerson =1Output: [0,1,2,3,5]Explanation: At time 0, person 0 shares the secret with person 1.At time 5, person 1 shares the secret with person 2.At time 8, person 2 shares the secret with person 3.At time 10, person 1 shares the secret with person 5.Thus, people 0,1,2,3, and 5 know the secret after all the meetings.
Input: n =4, meetings =[[3,1,3],[1,2,2],[0,3,3]], firstPerson =3Output: [0,1,3]Explanation:
At time 0, person 0 shares the secret with person 3.At time 2, neither person 1 nor person 2 know the secret.At time 3, person 3 shares the secret with person 0 and person 1.Thus, people 0,1, and 3 know the secret after all the meetings.
Input: n =5, meetings =[[3,4,2],[1,2,1],[2,3,1]], firstPerson =1Output: [0,1,2,3,4]Explanation:
At time 0, person 0 shares the secret with person 1.At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.Note that person 2 can share the secret at the same time as receiving it.At time 2, person 3 shares the secret with person 4.Thus, people 0,1,2,3, and 4 know the secret after all the meetings.
Meetings at the same time can spread the secret instantly among all connected people. We can process meetings in order of time, and for each time group, use union-find to connect people. After each group, only keep the secret for those connected to someone who already knows it.
classSolution {
public: vector<int> findAllPeople(int n, vector<vector<int>>& meetings, int firstPerson) {
vector<int> knows(n, 0);
knows[0] = knows[firstPerson] =1;
sort(meetings.begin(), meetings.end(), [](auto& a, auto& b){ return a[2] < b[2]; });
int m = meetings.size();
for (int i =0; i < m; ) {
int t = meetings[i][2], j = i;
unordered_map<int, int> par;
function<int(int)> find = [&](int x) {
if (!par.count(x)) par[x] = x;
if (par[x] != x) par[x] = find(par[x]);
return par[x];
};
for (; j < m && meetings[j][2] == t; ++j) {
int x = meetings[j][0], y = meetings[j][1];
par[find(x)] = find(y);
}
unordered_map<int, vector<int>> groups;
for (int k = i; k < j; ++k) {
int x = meetings[k][0], y = meetings[k][1];
groups[find(x)].push_back(x);
groups[find(y)].push_back(y);
}
for (auto& [root, group] : groups) {
bool hasSecret = false;
for (int x : group) if (knows[x]) { hasSecret = true; break; }
if (hasSecret) for (int x : group) knows[x] =1;
}
i = j;
}
vector<int> ans;
for (int i =0; i < n; ++i) if (knows[i]) ans.push_back(i);
return ans;
}
};
classSolution {
public List<Integer>findAllPeople(int n, int[][] meetings, int firstPerson) {
boolean[] knows =newboolean[n];
knows[0]= knows[firstPerson]=true;
Arrays.sort(meetings, (a, b) -> a[2]- b[2]);
for (int i = 0; i < meetings.length; ) {
int t = meetings[i][2], j = i;
Map<Integer, Integer> par =new HashMap<>();
java.util.function.Function<Integer, Integer> find =new java.util.function.Function<>() {
public Integer apply(Integer x) {
if (!par.containsKey(x)) par.put(x, x);
if (!par.get(x).equals(x)) par.put(x, apply(par.get(x)));
return par.get(x);
}
};
for (; j < meetings.length&& meetings[j][2]== t; ++j) {
int x = meetings[j][0], y = meetings[j][1];
par.put(find.apply(x), find.apply(y));
}
Map<Integer, List<Integer>> groups =new HashMap<>();
for (int k = i; k < j; ++k) {
int x = meetings[k][0], y = meetings[k][1];
groups.computeIfAbsent(find.apply(x), z ->new ArrayList<>()).add(x);
groups.computeIfAbsent(find.apply(y), z ->new ArrayList<>()).add(y);
}
for (var group : groups.values()) {
boolean hasSecret =false;
for (int x : group) if (knows[x]) { hasSecret =true; break; }
if (hasSecret) for (int x : group) knows[x]=true;
}
i = j;
}
List<Integer> ans =new ArrayList<>();
for (int i = 0; i < n; ++i) if (knows[i]) ans.add(i);
return ans;
}
}
classSolution:
deffindAllPeople(self, n: int, meetings: list[list[int]], firstPerson: int) -> list[int]:
from collections import defaultdict
knows = [False] * n
knows[0] = knows[firstPerson] =True meetings.sort(key=lambda x: x[2])
i =0while i < len(meetings):
t = meetings[i][2]
par = {}
deffind(x):
if x notin par:
par[x] = x
if par[x] != x:
par[x] = find(par[x])
return par[x]
j = i
while j < len(meetings) and meetings[j][2] == t:
x, y = meetings[j][0], meetings[j][1]
par[find(x)] = find(y)
j +=1 groups = defaultdict(list)
for k in range(i, j):
x, y = meetings[k][0], meetings[k][1]
groups[find(x)].append(x)
groups[find(y)].append(y)
for group in groups.values():
if any(knows[x] for x in group):
for x in group:
knows[x] =True i = j
return [i for i, v in enumerate(knows) if v]
use std::collections::HashMap;
impl Solution {
pubfnfind_all_people(n: i32, mut meetings: Vec<Vec<i32>>, first_person: i32) -> Vec<i32> {
let n = n asusize;
letmut knows =vec![false; n];
knows[0] =true;
knows[first_person asusize] =true;
meetings.sort_by_key(|x| x[2]);
letmut i =0;
while i < meetings.len() {
let t = meetings[i][2];
letmut par = HashMap::new();
fnfind(par: &mut HashMap<i32, i32>, x: i32) -> i32 {
let p =*par.get(&x).unwrap_or(&x);
if p != x {
let root = find(par, p);
par.insert(x, root);
root
} else {
x
}
}
letmut j = i;
while j < meetings.len() && meetings[j][2] == t {
let x = meetings[j][0];
let y = meetings[j][1];
let px = find(&mut par, x);
let py = find(&mut par, y);
par.insert(px, py);
j +=1;
}
letmut groups: HashMap<i32, Vec<i32>>= HashMap::new();
for k in i..j {
let x = meetings[k][0];
let y = meetings[k][1];
let px = find(&mut par, x);
let py = find(&mut par, y);
groups.entry(px).or_default().push(x);
groups.entry(py).or_default().push(y);
}
for group in groups.values() {
if group.iter().any(|&x| knows[x asusize]) {
for&x in group {
knows[x asusize] =true;
}
}
}
i = j;
}
(0..n).filter(|&i| knows[i]).map(|i| i asi32).collect()
}
}
⏰ Time complexity: O(m log m + m α(n)), where m is the number of meetings and n is the number of people. Sorting and union-find operations are efficient.
🧺 Space complexity: O(n + m), for the union-find structures and result.