The company structure forms a tree with the head of the company as the root. Each manager must inform their direct subordinates, who then inform their subordinates, and so on. The total time to inform everyone equals the longest path from the root to any leaf, where each edge weight is the manager's inform time.
We can traverse this tree using dfs, tracking the accumulated time as we go deeper. At each node, we add that manager's inform time before visiting their subordinates. The answer is the maximum time across all leaf nodes.
dfs from the head of the company.informTime[node] plus the maximum time from all its children.class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
adj = [[] for _ in range(n)]
for i in range(n):
if i != headID:
adj[manager[i]].append(i)
def dfs(node):
res = 0
for child in adj[node]:
res = max(res, informTime[node] + dfs(child))
return res
return dfs(headID)Instead of using recursion, we can traverse the tree level by level using bfs. We process nodes in waves, tracking the time at which each employee receives the news. The maximum time across all employees gives us the answer.
Each node in the queue stores both the employee ID and the time at which they were informed. When we process a node, we inform all their direct reports, adding the manager's inform time to compute when each subordinate receives the news.
0.class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
adj = defaultdict(list)
for i in range(n):
adj[manager[i]].append(i)
q = deque([(headID, 0)]) # (id, time)
res = 0
while q:
node, time = q.popleft()
res = max(res, time)
for emp in adj[node]:
q.append((emp, time + informTime[node]))
return resWe can reverse our thinking: instead of propagating time down from the root, we can compute times bottom-up starting from leaf employees. Leaf employees have an indegree of 0 (no one reports to them). They propagate their total time upward to their managers.
A manager's total time is the maximum among all their subordinates' times plus their own inform time. We process employees in topological order, from leaves toward the root. When all of a manager's subordinates have been processed, we can compute and propagate the manager's time.
indegree of each employee (how many people report to them).indegree = 0).indegree; if it becomes 0, add them to the queue.class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
indegree = [0] * n
time = [0] * n
for i in range(n):
if manager[i] != -1:
indegree[manager[i]] += 1
queue = deque()
for i in range(n):
if indegree[i] == 0:
queue.append(i)
while queue:
node = queue.popleft()
time[node] += informTime[node]
if manager[node] != -1:
time[manager[node]] = max(time[manager[node]], time[node])
indegree[manager[node]] -= 1
if indegree[manager[node]] == 0:
queue.append(manager[node])
return time[headID]Instead of building an explicit adjacency list, we can traverse from each employee up to the root, caching results along the way. For any employee, their total inform time is their own inform time plus their manager's total inform time.
We use the manager array directly for traversal and cache computed results in the inform time array itself. Once an employee's path to root is computed, we mark their manager as -1 to indicate completion. This approach uses path compression similar to Union-Find.
-1), return their inform time.-1 to cache the result and prevent recomputation.class Solution:
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
def dfs(node):
if manager[node] != -1:
informTime[node] += dfs(manager[node])
manager[node] = -1
return informTime[node]
res = 0
for node in range(n):
res = max(res, dfs(node))
return resThe total time is the longest path from root to any leaf, not the sum of all inform times. When a manager informs multiple subordinates, they are informed simultaneously. You should take the maximum time among all subtrees, not add them together. Using sum instead of max produces answers that are way too large.
Employees with no subordinates (leaf nodes) have informTime[i] = 0. When building the adjacency list or computing times, ensure you handle this correctly. Some solutions mistakenly add inform time even for leaves or skip leaves entirely, leading to incorrect results.
The manager array gives parent pointers, but for DFS/BFS you need children pointers. A common mistake is iterating incorrectly when building the adjacency list or accidentally including the head in some manager's list. The head has manager[headID] = -1, so you must skip adding edges for the head node.