|
| 1 | +## Explanation |
| 2 | + |
| 3 | +### Strategy (The "Why") |
| 4 | + |
| 5 | +**Restate the problem:** We have n people, and person 0 initially knows a secret. At time 0, person 0 shares the secret with `firstPerson`. Then, through a series of meetings at different times, the secret spreads. When two people meet at the same time, if either knows the secret, both learn it. However, the secret can only spread within the same time frame - connections formed at one time don't carry over to future times unless those people remain connected to person 0. |
| 6 | + |
| 7 | +**1.1 Constraints & Complexity:** |
| 8 | + |
| 9 | +- **Input Size:** n can be up to 10^5, and there can be up to 10^5 meetings. |
| 10 | +- **Time Complexity:** O(M log M) where M is the number of meetings - we sort meetings by time (O(M log M)) and process them with Union-Find operations (nearly O(1) per operation). |
| 11 | +- **Space Complexity:** O(n) for the Union-Find data structure. |
| 12 | +- **Edge Case:** If no meetings occur, only person 0 and firstPerson know the secret. |
| 13 | + |
| 14 | +**1.2 High-level approach:** |
| 15 | + |
| 16 | +The goal is to track which people know the secret by processing meetings in chronological order. At each time step, we temporarily connect people who meet, let the secret spread within that group, then only keep connections to person 0. People not connected to person 0 "forget" their temporary connections. |
| 17 | + |
| 18 | +**1.3 Brute force vs. optimized strategy:** |
| 19 | + |
| 20 | +- **Brute Force:** For each time, create a graph of all meetings, run BFS/DFS to find all connected components that include person 0, then reset. This would be O(M \* n) in worst case. |
| 21 | +- **Optimized Strategy:** Use Union-Find to efficiently connect people at the same time, then reset only those not connected to person 0. This is O(M log M) due to sorting. |
| 22 | +- **Optimization:** Union-Find provides nearly constant-time union and find operations, making it ideal for this dynamic connectivity problem. |
| 23 | + |
| 24 | +**1.4 Decomposition:** |
| 25 | + |
| 26 | +1. Sort all meetings by time to process them chronologically. |
| 27 | +2. Initialize Union-Find data structure and connect person 0 with firstPerson. |
| 28 | +3. For each unique time, process all meetings at that time by connecting the participants. |
| 29 | +4. After processing each time block, reset connections for people not connected to person 0. |
| 30 | +5. Return all people who are connected to person 0 after all meetings. |
| 31 | + |
| 32 | +### Steps (The "How") |
| 33 | + |
| 34 | +**2.1 Initialization & Example Setup:** |
| 35 | + |
| 36 | +Let's use the example: `n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1` |
| 37 | + |
| 38 | +- Initially, person 0 and person 1 know the secret. |
| 39 | +- We initialize parent array: `parent = [0, 1, 2, 3, 4, 5]` |
| 40 | +- After connecting 0 and 1: `parent = [0, 0, 2, 3, 4, 5]` |
| 41 | + |
| 42 | +**2.2 Start Processing:** |
| 43 | + |
| 44 | +We sort meetings by time: `[[1,2,5], [2,3,8], [1,5,10]]` (already sorted). |
| 45 | + |
| 46 | +**2.3 Trace Walkthrough:** |
| 47 | + |
| 48 | +| Time | Meeting | Action | Parent Array After Union | After Reset | |
| 49 | +| ---- | -------- | ------------- | ------------------------ | ------------------------------------------------ | |
| 50 | +| 5 | [1,2,5] | Union 1 and 2 | [0, 0, 0, 3, 4, 5] | [0, 0, 0, 3, 4, 5] (all connected to 0) | |
| 51 | +| 8 | [2,3,8] | Union 2 and 3 | [0, 0, 0, 0, 4, 5] | [0, 0, 0, 0, 4, 5] (all connected to 0) | |
| 52 | +| 10 | [1,5,10] | Union 1 and 5 | [0, 0, 0, 0, 4, 0] | [0, 0, 0, 0, 4, 0] (all except 4 connected to 0) | |
| 53 | + |
| 54 | +**2.4 Increment and Loop:** |
| 55 | + |
| 56 | +After processing each time block, we check which people are still connected to person 0. Those not connected have their parent reset to themselves, effectively "forgetting" the temporary connection. |
| 57 | + |
| 58 | +**2.5 Return Result:** |
| 59 | + |
| 60 | +The result is `[0, 1, 2, 3, 5]` - all people connected to person 0 after all meetings have been processed. |
0 commit comments