Problem solution · Python

Maximum Employees to Be Invited to a Meeting

Maximum Employees to Be Invited to a Meeting: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Breadth-first search
Source
walkccc LeetCode Solutions
Length
67 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

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.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Employees to Be Invited to a Meeting · PythonPython
Use this to learn the idea, then write your own version.
from enum import Enum  class State(Enum):  INIT = 0  VISITING = 1  VISITED = 2  class Solution:  def maximumInvitations(self, favorite: list[int]) -> int:    n = len(favorite)    sumComponentsLength = 0  # the component: a -> b -> c <-> x <- y    graph = [[] for _ in range(n)]    inDegrees = [0] * n    maxChainLength = [1] * n     # Build the graph.    for i, f in enumerate(favorite):      graph[i].append(f)      inDegrees[f] += 1     # Perform topological sorting.    q = collections.deque([i for i, d in enumerate(inDegrees) if d == 0])     while q:      u = q.popleft()      for v in graph[u]:        inDegrees[v] -= 1        if inDegrees[v] == 0:          q.append(v)        maxChainLength[v] = max(maxChainLength[v], 1 + maxChainLength[u])     for i in range(n):      if favorite[favorite[i]] == i:        # i <-> favorite[i] (the cycle's length = 2)        sumComponentsLength += maxChainLength[i] + maxChainLength[favorite[i]]     maxCycleLength = 0  # Cycle: a -> b -> c -> a    parent = [-1] * n    seen = set()    states = [State.INIT] * n     def findCycle(u: int) -> None:      nonlocal maxCycleLength      seen.add(u)      states[u] = State.VISITING      for v in graph[u]:        if v not in seen:          parent[v] = u          findCycle(v)        elif states[v] == State.VISITING:          # Find the cycle's length.          curr = u          cycleLength = 1          while curr != v:            curr = parent[curr]            cycleLength += 1          maxCycleLength = max(maxCycleLength, cycleLength)      states[u] = State.VISITED     for i in range(n):      if i not in seen:        findCycle(i)     return max(sumComponentsLength // 2, maxCycleLength) 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗