Time Needed to Inform All Employees (LeetCode 1376): Solution and Explanation

Medium
Tree
Depth First Search
Breadth First Search

A company forms a tree under its head. Each manager needs informTime minutes to tell all of their direct reports, and every branch spreads the news at the same time. Return how many minutes pass before the last employee hears it.

Intuition

Look at a single employee. The news travels down the one chain of managers above them, and each manager on that chain adds their informTime. So the minute this employee hears the news is the sum of informTime over all of their ancestors.

Different branches do not wait for each other. While one manager is telling their reports, every other manager is doing the same in parallel. That means the total time is not a sum over the whole company; it is the slowest single chain from the head to an employee.

So the problem is the longest root-to-node path in a weighted tree, where each edge from a manager to a report weighs the manager's informTime. Build a children list from the manager array, walk down from the head while carrying the running time, and keep the maximum.

Approach: BFS from the head with a running time

  1. Build children[m], the list of direct reports of each manager, from the manager array.
  2. Start a queue with (headID, 0): the head knows the news at minute 0.
  3. Pop (boss, t) and update best = max(best, t).
  4. For each report c of boss, push (c, t + informTime[boss]).
  5. When the queue is empty, every employee has been visited once; return best.

Step-by-step example

n = 8, headID = 4, manager = [2,2,4,6,-1,4,4,5], informTime = [0,0,4,0,7,3,6,0]
  1. Children: 4 manages [2, 5, 6], 2 manages [0, 1], 5 manages [7], 6 manages [3].
  2. Pop (4, 0). The head needs 7 minutes, so 2, 5 and 6 all hear at minute 7.
  3. Pop (2, 7): 0 and 1 hear at 7 + 4 = 11. Pop (5, 7): 7 hears at 7 + 3 = 10. Pop (6, 7): 3 hears at 7 + 6 = 13.
  4. Pop the leaves 0, 1, 7 and 3 at minutes 11, 11, 10 and 13. The largest is 13.
  5. The slowest chain is 4 → 6 → 3, costing 7 + 6 = 13 minutes. Return 13.

Code: Python, Java, C++

1class Solution:
2    def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
3        children = [[] for _ in range(n)]
4        for e in range(n):
5            if manager[e] != -1:
6                children[manager[e]].append(e)
7        best = 0
8        queue = deque([(headID, 0)])
9        while queue:
10            boss, t = queue.popleft()
11            best = max(best, t)
12            for c in children[boss]:
13                queue.append((c, t + informTime[boss]))
14        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Building the children lists touches each employee once, and the BFS pops each employee exactly once.

Space

O(n)

The children lists hold n - 1 entries in total, and the queue can hold up to n employees.

Other approaches

Recursive DFS (time O(n), space O(n))

Define time(v) = informTime[v] + max(time(c)) over the reports c of v, with leaves returning 0. It is short, but a chain of 100,000 managers overflows the default recursion stack in Python and Java.

Bottom-up walk with memo (time O(n), space O(n))

For every employee, walk up the manager array to the head, summing informTime, and cache each employee's answer so shared chains are walked once. No children list is needed.

Common mistakes

  • Adding up informTime over the whole company. Managers inform their reports in parallel, so only the slowest chain counts.
  • Adding the informTime of the employee being reached instead of their manager's. The delay belongs to whoever is doing the telling.
  • Using recursion on a deep chain, which can overflow the call stack for n up to 100,000.
  • Assuming the head is employee 0. The head is headID, the one employee whose manager is -1.

Frequently asked questions

Why is the answer the maximum and not the sum?

Every manager starts telling their reports as soon as they hear the news, independently of other branches. The company is fully informed when the last person hears it, which is the slowest root-to-employee chain.

Does it matter whether I use BFS or DFS?

No. Each employee's time depends only on their chain of managers, so any traversal that carries the running sum gets the same answer. BFS with a queue avoids recursion depth problems.

Why is this filed under graphs?

The manager array describes a tree, which is a graph with no cycles. The task is the longest weighted path from the root, a classic graph traversal.

What is the time complexity?

O(n) time and O(n) space, because every employee is added to the children list once and popped from the queue once.