You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2000-2099/2092.Find All People With Secret/README_EN.md
+73-44Lines changed: 73 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,21 @@ Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
88
88
89
89
<!-- solution:start -->
90
90
91
-
### Solution 1
91
+
### Solution 1: Simulation + Graph Traversal
92
+
93
+
The core idea of this problem is to find all people who eventually know the secret by simulating the propagation process through meetings. We can treat each meeting as an edge in an undirected graph, where each participant is a node in the graph, and the meeting time is the "timestamp" of the edge connecting the nodes. At each time point, we traverse all meetings and use Breadth-First Search (BFS) to simulate the propagation of the secret.
94
+
95
+
We create a boolean array $\textit{vis}$ to record whether each person knows the secret. Initially, $\textit{vis}[0] = \text{true}$ and $\textit{vis}[\textit{firstPerson}] = \text{true}$, indicating that person 0 and $\textit{firstPerson}$ already know the secret.
96
+
97
+
We first sort the meetings by time to ensure that we process meetings in the correct order during each traversal.
98
+
99
+
Next, we process each group of meetings at the same time. For each group of meetings at the same time (i.e., $\textit{meetings}[i][2] = \textit{meetings}[i+1][2]$), we treat them as events occurring at the same moment. For each meeting, we record the relationships between participants and add these participants to a set.
100
+
101
+
Then, we use BFS to propagate the secret. For all participants at the current time point, if any of them already know the secret, we will traverse the adjacent nodes of that participant (i.e., other participants in the meeting) through BFS and mark them as knowing the secret. This process continues to propagate until all people who can know the secret at this meeting are updated.
102
+
103
+
When all meetings are processed, we traverse the $\textit{vis}$ array, add the IDs of all people who know the secret to the result list, and return it.
104
+
105
+
The time complexity is $O(m \times \log m + n)$, and the space complexity is $O(n)$, where $m$ and $n$ are the number of meetings and the number of experts, respectively.
0 commit comments