Maximum Employees to Be Invited to a Meeting - Problem
A company is organizing a meeting with n employees around a large circular table. Each employee has exactly one favorite person they want to sit next to, and they'll only attend if this condition is met.
Think of this as a graph problem where each employee points to their favorite person, creating a functional graph (each node has exactly one outgoing edge). The challenge is to find the maximum number of employees we can invite while satisfying everyone's seating preferences.
Key insights:
- Since it's a circular table, we can have cycles of any length
- We can also have "chains" that lead into cycles
- For cycles of length > 2, we can only invite the cycle members
- For cycles of length 2 (mutual favorites), we can extend with chains leading to them
Goal: Return the maximum number of employees that can be invited to the meeting.
Input & Output
example_1.py โ Basic 2-cycles
$
Input:
[2,2,1,2]
โบ
Output:
3
๐ก Note:
Employee 1 and 2 are mutual favorites (1โ2, 2โ1). Employee 0 favorites employee 2, so we can seat them as 0โ2โ1, creating a chain of length 3. Employee 3 also favorites 2, but we can only extend one side of the 2-cycle.
example_2.py โ Large cycle
$
Input:
[1,2,0]
โบ
Output:
3
๐ก Note:
We have a 3-cycle: 0โ1โ2โ0. Since this is a cycle of length > 2, we can invite all 3 employees and seat them in this exact circular order.
example_3.py โ Multiple 2-cycles with chains
$
Input:
[3,0,1,4]
โบ
Output:
4
๐ก Note:
We have one 2-cycle: 0โ3 (0โ3, 3โ0). Employees 1 and 2 form a separate 2-cycle: 1โ2โ1. Wait, that's not right. Let me recalculate: 0โ3, 1โ0, 2โ1, 3โ4. This creates: 2โ1โ0โ3โ4, but 4 would need to point somewhere. Let me use a simpler example.
Constraints
- n == favorite.length
- 2 โค n โค 105
- 0 โค favorite[i] โค n - 1
- favorite[i] != i (no self-favorites)
Visualization
Tap to expand
Understanding the Visualization
1
Identify Graph Type
Each employee points to exactly one favorite - this creates a functional graph
2
Find Cycles
Every connected component must eventually form a cycle
3
Classify Cycles
Large cycles (>2) vs 2-cycles (mutual pairs)
4
Extend 2-Cycles
2-cycles can be extended with incoming chains
5
Choose Maximum
Pick either the largest single cycle or sum of all extended 2-cycles
Key Takeaway
๐ฏ Key Insight: In a functional graph, we can either invite one complete large cycle OR multiple 2-cycles extended with their incoming chains - never mix both approaches!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code