Skip to content

2127. Maximum Employees to Be Invited to a Meeting #1226

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

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

<?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] …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Jan 26, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jan 26, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants