Skip to content

Commit 8ca9dd3

Browse files
committed
Sync LeetCode submission Runtime - 159 ms (93.75%), Memory - 31.5 MB (70.67%)
1 parent 276e846 commit 8ca9dd3

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<p>A company is organizing a meeting and has a list of <code>n</code> employees, waiting to be invited. They have arranged for a large <strong>circular</strong> table, capable of seating <strong>any number</strong> of employees.</p>
2+
3+
<p>The employees are numbered from <code>0</code> to <code>n - 1</code>. Each employee has a <strong>favorite</strong> person and they will attend the meeting <strong>only if</strong> they can sit next to their favorite person at the table. The favorite person of an employee is <strong>not</strong> themself.</p>
4+
5+
<p>Given a <strong>0-indexed</strong> integer array <code>favorite</code>, where <code>favorite[i]</code> denotes the favorite person of the <code>i<sup>th</sup></code> employee, return <em>the <strong>maximum number of employees</strong> that can be invited to the meeting</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/14/ex1.png" style="width: 236px; height: 195px;" />
10+
<pre>
11+
<strong>Input:</strong> favorite = [2,2,1,2]
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong>
14+
The above figure shows how the company can invite employees 0, 1, and 2, and seat them at the round table.
15+
All employees cannot be invited because employee 2 cannot sit beside employees 0, 1, and 3, simultaneously.
16+
Note that the company can also invite employees 1, 2, and 3, and give them their desired seats.
17+
The maximum number of employees that can be invited to the meeting is 3.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> favorite = [1,2,0]
24+
<strong>Output:</strong> 3
25+
<strong>Explanation:</strong>
26+
Each employee is the favorite person of at least one other employee, and the only way the company can invite them is if they invite every employee.
27+
The seating arrangement will be the same as that in the figure given in example 1:
28+
- Employee 0 will sit between employees 2 and 1.
29+
- Employee 1 will sit between employees 0 and 2.
30+
- Employee 2 will sit between employees 1 and 0.
31+
The maximum number of employees that can be invited to the meeting is 3.
32+
</pre>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/14/ex2.png" style="width: 219px; height: 220px;" />
36+
<pre>
37+
<strong>Input:</strong> favorite = [3,0,1,4,1]
38+
<strong>Output:</strong> 4
39+
<strong>Explanation:</strong>
40+
The above figure shows how the company will invite employees 0, 1, 3, and 4, and seat them at the round table.
41+
Employee 2 cannot be invited because the two spots next to their favorite employee 1 are taken.
42+
So the company leaves them out of the meeting.
43+
The maximum number of employees that can be invited to the meeting is 4.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>n == favorite.length</code></li>
51+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
52+
<li><code>0 &lt;= favorite[i] &lt;=&nbsp;n - 1</code></li>
53+
<li><code>favorite[i] != i</code></li>
54+
</ul>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Approach 2: Topological Sort to Reduce Non-Cyclic Nodes
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
from collections import deque
7+
8+
class Solution:
9+
def maximumInvitations(self, favorite: List[int]) -> int:
10+
n = len(favorite)
11+
in_degree = [0] * n
12+
13+
# calculate in-degree for each node
14+
for person in range(n):
15+
in_degree[favorite[person]] += 1
16+
17+
# topological sorting to remove non-cycle nodes
18+
q = deque()
19+
for person in range(n):
20+
if in_degree[person] == 0:
21+
q.append(person)
22+
depth = [1] * n # depth for each node
23+
24+
while q:
25+
current_node = q.popleft()
26+
next_node = favorite[current_node]
27+
depth[next_node] = max(depth[next_node], depth[current_node] + 1)
28+
in_degree[next_node] -= 1
29+
if in_degree[next_node] == 0:
30+
q.append(next_node)
31+
32+
longest_cycle = 0
33+
two_cycle_invitations = 0
34+
35+
# Detect cycles
36+
for person in range(n):
37+
if in_degree[person] == 0: # Already processed
38+
continue
39+
40+
cycle_length = 0
41+
current = person
42+
43+
while in_degree[current] != 0:
44+
in_degree[current] = 0 # mark as visited
45+
cycle_length += 1
46+
current = favorite[current]
47+
48+
if cycle_length == 2:
49+
# for 2-cycles, add the depth of both nodes
50+
two_cycle_invitations += depth[person] + depth[favorite[person]]
51+
else:
52+
longest_cycle = max(longest_cycle, cycle_length)
53+
54+
return max(longest_cycle, two_cycle_invitations)
55+

0 commit comments

Comments
 (0)