forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-employees-to-be-invited-to-a-meeting.py
42 lines (39 loc) · 1.3 KB
/
maximum-employees-to-be-invited-to-a-meeting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Time: O(n)
# Space: O(n)
class Solution(object):
def maximumInvitations(self, favorite):
"""
:type favorite: List[int]
:rtype: int
"""
def find_cycles(adj):
result = []
lookup = [False]*len(adj)
for u in xrange(len(adj)):
cnt = {}
while not lookup[u]:
lookup[u] = True
cnt[u] = len(cnt)
u = adj[u]
if u in cnt:
result.append((u, len(cnt)-cnt[u]))
return result
def bfs(adj, u, exclude):
result = 0
q = [u]
while q:
result += 1
new_q = []
for u in q:
for v in adj[u]:
if v == exclude:
continue
new_q.append(v)
q = new_q
return result
inv_adj = [[] for _ in xrange(len(favorite))]
for u, v in enumerate(favorite):
inv_adj[v].append(u)
cycles = find_cycles(favorite)
return max(max([l for _, l in cycles if l > 2] or [0]),
sum(bfs(inv_adj, u, favorite[u]) + bfs(inv_adj, favorite[u], u) for u, l in cycles if l == 2))