Description

A company is organizing a meeting and has a list of n employees, waiting to be invited. They have arranged for a large circular table, capable of seating any number of employees.

The employees are numbered from 0 to n - 1. Each employee has a favorite person and they will attend the meeting only if they can sit next to their favorite person at the table. The favorite person of an employee is not themself.

Given a 0-indexed integer array favorite, where favorite[i] denotes the favorite person of the ith employee, return the maximum number of employees that can be invited to the meeting.

Idea

Solution proposed by Shubham Baban Shinde

1. Large Cycles (Size >2)

Example:
A → B → C → A (cycle of 3)
Explanation:

  • Each person must sit next to their favorite.
  • Seating: A next to B, B next to C, C next to A (circular).
  • All 3 can attend.
    Result:
    Maximum invitations = cycle length (3).

2. Mutual Pairs (Cycle of 2) with Chains

Example:
A ↔ B (mutual favorites), with chains:
C → A and D → E → B
Explanation:

  • A and B sit together.
  • Chain C → A adds 1 person (C sits next to A).
  • Chain D → E → B adds 2 people (D next to E, E next to B).
    Seating: C-A-B-E-D (circular).
    Result:
    Total = 2 (pair) + 1 (C) + 2 (D,E) = 5.

Why Cycle Length 2 is Special

  • Mutual pairs (A ↔ B) allow combining two independent chains.
  • Larger cycles (like A→B→C→A) can’t add external people—every seat is already taken by cycle members.

Key Code Steps Explained

  1. Track Dependencies (inDeg):
    • Count how many employees favor each person.
    • Example: If B is favorited by A and C, inDeg[B] = 2.
  2. Process Chains (Queue):
    • Employees with inDeg = 0 start chains (e.g., C in C→A).
    • Propagate chain lengths (e.g., C→A increases A’s chain length to 1).
  3. Detect Cycles:
    • Unvisited employees after chain processing form cycles.
    • For cycles of 2 (A ↔ B), add both employees plus their chains.
    • For larger cycles, track the longest one.
  4. Final Decision:
    • Compare the largest cycle size with the sum of all mutual pairs and their chains.

Code

from collections import deque
 
class Solution(object):
    def maximumInvitations(self, favorite):
        n = len(favorite)
        in_deg = [0] * n
        chain_len = [0] * n
        visited = [False] * n
        q = deque()
        
        # Count how many people favor each employee
        for f in favorite:
            in_deg[f] += 1
        
        # Start with employees no one favorites (chain starters)
        for i in range(n):
            if in_deg[i] == 0:
                q.append(i)
        
        # Process chains to calculate max chain lengths
        while q:
            u = q.popleft()
            visited[u] = True
            v = favorite[u]
            chain_len[v] = max(chain_len[v], chain_len[u] + 1)
            in_deg[v] -= 1
            if in_deg[v] == 0:
                q.append(v)
        
        max_cycle, pair_chains = 0, 0
        
        # Detect cycles and calculate results
        for i in range(n):
            if visited[i]:
                continue
            cycle_len = 0
            current = i
            # Measure cycle length
            while not visited[current]:
                visited[current] = True
                current = favorite[current]
                cycle_len += 1
            if cycle_len == 2:  # Mutual pair
                pair_chains += 2 + chain_len[i] + chain_len[favorite[i]]
            else:
                max_cycle = max(max_cycle, cycle_len)
        
        return max(max_cycle, pair_chains)