-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPeopleWithSecrets.java
More file actions
59 lines (46 loc) · 1.57 KB
/
AllPeopleWithSecrets.java
File metadata and controls
59 lines (46 loc) · 1.57 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
50
51
52
53
54
55
56
57
58
59
package graph;
import java.util.*;
public class AllPeopleWithSecrets {
public static List<Integer> findAllPeople(int n, int[][] meetings, int firstPerson) {
ArrayList<ArrayList<Pair>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
for (int[] meeting : meetings) {
graph.get(meeting[0]).add(new Pair(meeting[1], meeting[2]));
graph.get(meeting[1]).add(new Pair(meeting[0], meeting[2]));
}
// Secret knowing time initialization!
int[] secretTime = new int[n];
Arrays.fill(secretTime, Integer.MAX_VALUE);
secretTime[0] = 0;
secretTime[firstPerson] = 0;
Queue<Integer> q = new LinkedList<>();
q.add(0);
q.add(firstPerson);
while (!q.isEmpty()) {
int curr = q.poll();
ArrayList<Pair> temp = graph.get(curr);
if (temp.isEmpty()) continue;
for (Pair pair : temp) {
if (secretTime[curr] <= pair.time && secretTime[pair.dest] > pair.time) {
secretTime[pair.dest] = pair.time;
q.add(pair.dest);
}
}
}
ArrayList<Integer> list = new ArrayList<>();
int i = 0;
for (int num : secretTime) {
if (num != Integer.MAX_VALUE) list.add(i);
i++;
}
return list;
}
static class Pair {
int dest;
int time;
public Pair(int dest, int time) {
this.dest = dest;
this.time = time;
}
}
}