Find All People With Secret - Problem

Imagine you're tracking the spread of confidential information through a network of meetings! 🕵️

You have n people numbered from 0 to n-1. Person 0 knows a secret and initially shares it with firstPerson at time 0.

You're given an array meetings where meetings[i] = [xi, yi, timei] means person xi and person yi meet at time timei. When someone who knows the secret meets someone else, they instantly share the secret!

Key insight: If multiple meetings happen at the same time, the secret can spread through all connected people in that time frame before moving to the next time.

Goal: Return all people who know the secret after all meetings are complete.

Input & Output

example_1.py — Basic Secret Spreading
$ Input: n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
Output: [0, 1, 2, 3]
💡 Note: Person 0 tells person 1 initially. At time 5, person 1 meets person 2 and shares the secret. At time 8, person 2 meets person 3 and shares the secret. Person 5 meets person 1 at time 10 but person 5 doesn't initially know the secret, so they learn it then.
example_2.py — Simultaneous Meetings
$ Input: n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
Output: [0, 1, 3]
💡 Note: Person 0 tells person 3 initially. At time 2, person 1 meets person 2, but neither knows the secret. At time 3, person 3 meets person 1 (learning the secret) and person 0 meets person 3 simultaneously. This creates a connected component where the secret spreads to person 1.
example_3.py — No Additional Spread
$ Input: n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
Output: [0, 1, 2, 3, 4]
💡 Note: Person 0 tells person 1 initially. At time 1, persons 1-2 meet and 2-3 meet simultaneously, so the secret spreads to persons 2 and 3. At time 2, persons 3-4 meet, so person 4 learns the secret.

Constraints

  • 2 ≤ n ≤ 105
  • 0 ≤ meetings.length ≤ 105
  • meetings[i].length == 3
  • 0 ≤ xi, yi ≤ n - 1
  • xi ≠ yi
  • 1 ≤ timei ≤ 105
  • 1 ≤ firstPerson ≤ n - 1
  • Each meeting involves exactly two distinct people

Visualization

Tap to expand
Find All People With Secret INPUT People Network (n=6) 0 1 2 3 4 5 Meetings Array: [1,2,5] [2,3,8] [1,5,10] n = 6 firstPerson = 1 meetings = [ [1,2,5],[2,3,8],[1,5,10] ] ALGORITHM STEPS 1 Initialize 0 and 1 know secret at t=0 2 Sort by Time Group meetings by timestamp 3 Union-Find Connect people at same time 4 Propagate Secret Check connected components Timeline: t=0 0--1 t=5 1--2 t=8 2--3 t=10 1--5 Secret Spread: t=0: {0,1} know t=5: 1 meets 2 --> {0,1,2} t=8: 2 meets 3 --> {0,1,2,3} FINAL RESULT Final Network State 0 KNOWS 1 KNOWS 2 KNOWS 3 KNOWS 4 NO 5 NO Output: [0, 1, 2, 3] OK - 4 people know secret Person 5 met 1 at t=10 but 1 already shared with 2,3 Key Insight: Use Union-Find to group people meeting at the same time. Process meetings in chronological order. At each timestamp, connect all meeting participants, then check if any group contains someone with the secret. Reset non-knowing groups after each time to prevent false propagation. Time: O(M log M), Space: O(N+M) TutorialsPoint - Find All People With Secret | Optimal Solution (Union-Find)
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen