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]
๐Ÿ’ก 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. Person 4 is not connected to anyone with the secret at this point.

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
Secret Spreading Through Meeting NetworkTimeline:t=0t=1t=2t=30123meeting24243Key Insights๐Ÿ”น Process meetings chronologically by timestamp๐Ÿ”น Group simultaneous meetings into connected components๐Ÿ”น Secret spreads instantly through entire components๐Ÿ”น Union-Find efficiently manages component connectionsFinal Result: Everyone Connected to the Secret NetworkUnion-Find ensures optimal O(m log m + m ฮฑ(n)) time complexity
Understanding the Visualization
1
Initial State
Person 0 and firstPerson know the secret
2
Time-based Processing
Group meetings by timestamp and process chronologically
3
Component Formation
Use Union-Find to connect people meeting at same time
4
Secret Propagation
Spread secret through entire connected components
5
Final Result
All people who learned the secret through the network
Key Takeaway
๐ŸŽฏ Key Insight: Use Union-Find to efficiently group people meeting simultaneously, then propagate secrets through entire connected components at once. This handles the instantaneous spreading requirement optimally.
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