Problem
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.
Examples
Example 1:
graph LR; 0 --> 2 1 --> 2 2 --> 1 3 --> 2
|
|
Example 2:
graph LR; 0 --> 1 1 --> 2 2 --> 0
|
|
Example 3:
graph LR; 0 --> 3 1 --> 0 2 --> 1 3 --> 4 4 --> 1
|
|
Constraints:
n == favorite.length
2 <= n <= 105
0 <= favorite[i] <= n - 1
favorite[i] != i
Solution
Method 1 - Find the largest cycle
The key intuition is to treat employee favouritism as a directed graph problem where finding cycles is essential. In a meeting setup, cycles represent groups of employees that can sit next to each other seamlessly. Special attention to 2-cycles (mutual favourite pairs) allows us to extend seating by maximising possible chains leading into these pairs, thereby increasing the total number of employees who can be invited. The combined maximum of these cycles and chain extensions gives the optimal solution.
Approach
- Graph Representation:
- We represent the relationships using a directed graph where
favorite[i]
implies a directed edge from employeei
to the employee they favour.
- We represent the relationships using a directed graph where
- Detecting Cycles:
- We traverse the graph to detect cycles using Depth First Search (DFS). Detecting cycles helps us understand which groups of people can indeed form a complete seating arrangement. For each cycle, we record its length.
- A notable form of a cycle is the 2-cycle (mutual favourite pairs), where a pair of nodes point to each other. These 2-cycles need special consideration since additional employees can only be chained to these without breaking the cycle.
- Cycle Length Calculation:
- During this detection, we identify the longest cycle (
singleMaxCycleSize
), which defines the maximum number of people that can be seated cyclically without considering the extension possibilities.
- During this detection, we identify the longest cycle (
- Extending 2-Cycles:
- For each 2-cycle detected, we explore possible chains leading into these cycles. This means, from each node of a 2-cycle, we use DFS to find the longest possible path that can be added to this cycle without violating the seating rules.
- Depending on the chain length extending from both sides of the 2-cycle, we add these to the total count of employees that can be seated.
- Combining Results:
- Finally, we compare the longest cycle length found with the longest possible setting considering the combined extensions of all 2-cycles. The maximum value between these gives us the desired result.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the number of employeesn
, where each node (employee) and edge (relationship) is visited once during the graph traversal, detection of cycles, and exploration of possible extensions. - 🧺 Space complexity:
O(n)
. Space complexity is also linear mainly due to:- The graph representation, which requires storage for nodes and their relations.
- The additional structures such as
visited
arrays and dictionaries used to maintain states during DFS traversal and the extension calculations.