2127. Maximum Employees to Be Invited to a Meeting #1226
-
Topics: A company is organizing a meeting and has a list of The employees are numbered from Given a 0-indexed integer array Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The solution involves analyzing cycles and chains in the graph formed by the Let's implement this solution in PHP: 2127. Maximum Employees to Be Invited to a Meeting <?php
/**
* @param Integer[] $favorite
* @return Integer
*/
function maximumInvitations($favorite) {
$n = count($favorite);
// Step 1: Build graph and indegree array
$indegree = array_fill(0, $n, 0);
$graph = array_fill(0, $n, []);
for ($i = 0; $i < $n; $i++) {
$indegree[$favorite[$i]]++;
$graph[$favorite[$i]][] = $i;
}
// Step 2: Process chains (using topological sort to find chains)
$queue = [];
for ($i = 0; $i < $n; $i++) {
if ($indegree[$i] === 0) {
$queue[] = $i;
}
}
$chainLengths = array_fill(0, $n, 0);
while (!empty($queue)) {
$current = array_pop($queue);
$fav = $favorite[$current];
$chainLengths[$fav] = max($chainLengths[$fav], $chainLengths[$current] + 1);
if (--$indegree[$fav] === 0) {
$queue[] = $fav;
}
}
// Step 3: Process cycles
$visited = array_fill(0, $n, false);
$maxCycleLength = 0;
$totalChainSum = 0;
for ($i = 0; $i < $n; $i++) {
if (!$visited[$i]) {
$current = $i;
$cycleNodes = [];
// Detect cycle
while (!$visited[$current]) {
$visited[$current] = true;
$cycleNodes[] = $current;
$current = $favorite[$current];
}
// Check if we found a cycle
if (in_array($current, $cycleNodes)) {
$cycleStartIndex = array_search($current, $cycleNodes);
$cycle = array_slice($cycleNodes, $cycleStartIndex);
$cycleLength = count($cycle);
if ($cycleLength == 2) {
// Handle special case of 2-length cycles (merge chains)
$node1 = $cycle[0];
$node2 = $cycle[1];
$totalChainSum += $chainLengths[$node1] + $chainLengths[$node2] + 2;
} else {
// Regular cycle
$maxCycleLength = max($maxCycleLength, $cycleLength);
}
}
}
}
return max($maxCycleLength, $totalChainSum);
}
// Example usage:
$favorites1 = [2, 2, 1, 2];
$favorites2 = [1, 2, 0];
$favorites3 = [3, 0, 1, 4, 1];
echo maximumInvitations($favorites1) . "\n"; // Output: 3
echo maximumInvitations($favorites2) . "\n"; // Output: 3
echo maximumInvitations($favorites3) . "\n"; // Output: 4
?> Explanation:
This approach ensures efficiency with a time complexity of O(n), making it suitable for large inputs up to 105. |
Beta Was this translation helpful? Give feedback.
The solution involves analyzing cycles and chains in the graph formed by the
favorite
array.Let's implement this solution in PHP: 2127. Maximum Employees to Be Invited to a Meeting