2092. Find All People With Secret #2556
-
|
Topics: You are given an integer Person The secrets are shared instantaneously. That is, a person may receive the secret and share it with people in other meetings within the same time frame. Return a list of all the people that have the secret after all the meetings have taken place. You may return the answer in any order. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to efficiently track how the secret spreads through meetings over time, accounting for the fact that meetings at the same time can propagate the secret through multiple hops instantaneously. ApproachWe can solve this using a union-find (disjoint set) data structure with time-aware connections. The key insight is that:
Let's implement this solution in PHP: 2092. Find All People With Secret <?php
/**
* @param Integer $n
* @param Integer[][] $meetings
* @param Integer $firstPerson
* @return Integer[]
*/
function findAllPeople($n, $meetings, $firstPerson) {
// Sort meetings by time
usort($meetings, function($a, $b) {
return $a[2] - $b[2];
});
// Track who knows the secret
$knowsSecret = array_fill(0, $n, false);
$knowsSecret[0] = true;
$knowsSecret[$firstPerson] = true;
$m = count($meetings);
$i = 0;
while ($i < $m) {
$currentTime = $meetings[$i][2];
// Build graph for current time
$graph = [];
$peopleAtTime = [];
while ($i < $m && $meetings[$i][2] == $currentTime) {
$x = $meetings[$i][0];
$y = $meetings[$i][1];
if (!isset($graph[$x])) $graph[$x] = [];
if (!isset($graph[$y])) $graph[$y] = [];
$graph[$x][] = $y;
$graph[$y][] = $x;
$peopleAtTime[] = $x;
$peopleAtTime[] = $y;
$i++;
}
// Use BFS to propagate secret within this time's graph
$queue = new SplQueue();
$visited = [];
// Add all people who currently know the secret at this time
foreach ($peopleAtTime as $person) {
if ($knowsSecret[$person] && !isset($visited[$person])) {
$queue->enqueue($person);
$visited[$person] = true;
}
}
// BFS
while (!$queue->isEmpty()) {
$current = $queue->dequeue();
if (!isset($graph[$current])) continue;
foreach ($graph[$current] as $neighbor) {
if (!isset($visited[$neighbor])) {
$knowsSecret[$neighbor] = true;
$visited[$neighbor] = true;
$queue->enqueue($neighbor);
}
}
}
}
// Collect result
$result = [];
for ($i = 0; $i < $n; $i++) {
if ($knowsSecret[$i]) {
$result[] = $i;
}
}
return $result;
}
// Test cases
echo findAllPeople(6, [[1,2,5],[2,3,8],[1,5,10]], 1) . "\n"; // Output: [0,1,2,3,5]
echo findAllPeople(4, [[3,1,3],[1,2,2],[0,3,3]], 3) . "\n"; // Output: [0,1,3]
echo findAllPeople(5, [[3,4,2],[1,2,1],[2,3,1]], 1) . "\n"; // Output: [0,1,2,3,4]
?>Explanation:
Complexity Analysis
|
Beta Was this translation helpful? Give feedback.
We need to efficiently track how the secret spreads through meetings over time, accounting for the fact that meetings at the same time can propagate the secret through multiple hops instantaneously.
Approach
We can solve this using a union-find (disjoint set) data structure with time-aware connections. The key insight is that:
Let's implement this solution in PHP: 2092. Find All People With Secret