Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Find All People With Secret

XPChallenge Points: 30
levelLevel: Hard

You are given n people labeled from 0 to n-1. There is a list of meetings, where each meeting is represented as [a, b, t], meaning that person a meets person b at time t. A person can attend multiple meetings even if they happen at the same time.

Initially, person 0 knows a secret and immediately shares it with firstPerson at time 0. After this, whenever two people meet at time t, if either of them knows the secret at that moment, they instantly pass it to the other. Secret transfers within the same time group are also allowed.

Your task is to return every person who eventually learns the secret, in any order, after processing all meetings.

Example 1:

Input: n = 7, meetings = [[1,6,2],[2,3,2],[3,6,2],[4,5,10]], firstPerson = 6

Output: [0,6,1,3,2]

Explanation:

At time 0 → Person 0 shares the secret with 6 At time 2 → Chain reaction: 6 → 1 and 3 → 2 Time 10 meeting is irrelevant Final → 0,1,2,3,6

Example 2:

Input: n = 4, meetings = [[0,2,4],[1,2,4],[2,3,8]], firstPerson = 3

Output: [0,3]

Explanation:

Person 0 gives secret to 3 at time 0 No one has the secret at time 4 At time 8 → 3 gives it to 2 Final → 0,3

Example 3:

Input: n = 5, meetings = [[0,2,3],[2,4,3],[1,3,5]], firstPerson = 2

Output: [0,2,4]

Explanation:

At time 0 → Person 0 gives the secret to person 2 At time 3 → Person 2 meets person 0 and 4, so both have the secret At time 5 → Person 1 and 3 meet, but neither knows the secret Final secret holders → 0, 2, 4

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result