-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_All_People_With_Secret.java
More file actions
49 lines (42 loc) · 1.65 KB
/
Find_All_People_With_Secret.java
File metadata and controls
49 lines (42 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.io.*;
import java.lang.*;
public class Find_All_People_With_Secret {
class Solution {
public List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
Map<Integer, List<int[]>> graph = new HashMap<>();
for (int[] meeting : meetings) {
int x = meeting[0], y = meeting[1], t = meeting[2];
graph.computeIfAbsent(x, k -> new ArrayList<>()).add(new int[]{t, y});
graph.computeIfAbsent(y, k -> new ArrayList<>()).add(new int[]{t, x});
}
int[] vis = new int[n];
Arrays.fill(vis, Integer.MAX_VALUE);
vis[0] = 0;
vis[firstPerson] = 0;
Queue<int[]> q = new ArrayDeque<>();
q.offer(new int[]{0, 0});
q.offer(new int[]{firstPerson, 0});
while (!q.isEmpty()) {
int[] personTime = q.poll();
int person = personTime[0];
int time = personTime[1];
for (int[] nextPersonTime : graph.getOrDefault(person, new ArrayList<>())) {
int t = nextPersonTime[0];
int nextPerson = nextPersonTime[1];
if (t >= time && vis[nextPerson] > t) {
vis[nextPerson] = t;
q.offer(new int[]{nextPerson, t});
}
}
}
List<Integer> al = new ArrayList<>();
for (int i = 0; i < n; ++i) {
if (vis[i] != Integer.MAX_VALUE) {
al.add(i);
}
}
return al;
}
}
}