-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0268.py
33 lines (29 loc) · 1.35 KB
/
LC0268.py
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
class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
# For every person, store the time and label of the person met.
graph = defaultdict(list)
for x, y, t in meetings:
graph[x].append((t, y))
graph[y].append((t, x))
# Earliest time at which a person learned the secret
# as per current state of knowledge. If due to some new information,
# the earliest time of knowing the secret changes, we will update it
# and again process all the people whom he/she meets after the time
# at which he/she learned the secret.
earliest = [inf] * n
earliest[0] = 0
earliest[firstPerson] = 0
# Queue for BFS. It will store (person, time of knowing the secret)
q = deque()
q.append((0, 0))
q.append((firstPerson, 0))
# Do BFS
while q:
person, time = q.popleft()
for t, next_person in graph[person]:
if t >= time and earliest[next_person] > t:
earliest[next_person] = t
q.append((next_person, t))
# Since we visited only those people who know the secret,
# we need to return indices of all visited people.
return [i for i in range(n) if earliest[i] != inf]