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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code