Approach
Breadth-first search
For Maximum Employees to Be Invited to a Meeting, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 67 lines of Python from the credited upstream file 2127.py.
- The implementation visibly relies on sequence storage, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from enum import Enum2 3 4class State(Enum):5 INIT = 06 VISITING = 17 VISITED = 28 9 10class Solution:11 def maximumInvitations(self, favorite: list[int]) -> int:12 n = len(favorite)13 sumComponentsLength = 0 14 graph = [[] for _ in range(n)]15 inDegrees = [0] * n16 maxChainLength = [1] * n17 18 19 for i, f in enumerate(favorite):20 graph[i].append(f)21 inDegrees[f] += 122 23 24 q = collections.deque([i for i, d in enumerate(inDegrees) if d == 0])25 26 while q:27 u = q.popleft()28 for v in graph[u]:29 inDegrees[v] -= 130 if inDegrees[v] == 0:31 q.append(v)32 maxChainLength[v] = max(maxChainLength[v], 1 + maxChainLength[u])33 34 for i in range(n):35 if favorite[favorite[i]] == i:36 37 sumComponentsLength += maxChainLength[i] + maxChainLength[favorite[i]]38 39 maxCycleLength = 0 40 parent = [-1] * n41 seen = set()42 states = [State.INIT] * n43 44 def findCycle(u: int) -> None:45 nonlocal maxCycleLength46 seen.add(u)47 states[u] = State.VISITING48 for v in graph[u]:49 if v not in seen:50 parent[v] = u51 findCycle(v)52 elif states[v] == State.VISITING:53 54 curr = u55 cycleLength = 156 while curr != v:57 curr = parent[curr]58 cycleLength += 159 maxCycleLength = max(maxCycleLength, cycleLength)60 states[u] = State.VISITED61 62 for i in range(n):63 if i not in seen:64 findCycle(i)65 66 return max(sumComponentsLength 2, maxCycleLength)67