Maximum Employees to Be Invited to a Meeting

Hard Topics Company Tags

Employees are numbered from 0 through n-1, and favorite[i] is the person employee i wants to sit beside. Choose a subset of employees and arrange them around one circular table. Every chosen employee must have their favorite person immediately next to them on either side. Each employee can occupy only one seat, and the table can hold any number of people. Return the largest number you can seat while satisfying everyone. No employee lists themself as their favorite.

Example 1:

Maximum Employees to Be Invited to a Meeting, example 1: illustration of the example

Input: favorite = [1,0,0,1]

Output: 4

Explanation: Seat employees in circular order 2, 0, 1, 3. Employee 2 sits beside favorite 0, employees 0 and 1 sit beside each other, and employee 3 sits beside favorite 1. All four can attend.

Example 2:

Maximum Employees to Be Invited to a Meeting, example 2: illustration of the example

Input: favorite = [1,0,3,2]

Output: 4

Explanation: The favorite pairs are 0 with 1 and 2 with 3. Circular order 0, 1, 2, 3 keeps both pairs adjacent, so all four employees can attend.

Example 3:

Maximum Employees to Be Invited to a Meeting, example 3: illustration of the example

Input: favorite = [1,2,0,0]

Output: 3

Explanation: Employees 0, 1, and 2 can sit in that circular order. Their favorite relationships require all three edges of the triangle between them. Adding employee 3 beside 0 would break a required adjacency, so the maximum is 3.

Constraints:

  • 2 <= n <= 100000
  • 0 <= favorite[i] < n
  • favorite[i] != i


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.