Leetcode #1376: Time Needed to Inform All Employees
In this guide, we solve Leetcode #1376 Time Needed to Inform All Employees in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Tree, Depth-First Search, Breadth-First Search
Intuition
We need to explore a structure deeply before backing up, which suits DFS.
DFS keeps local context on the call stack and is easy to implement recursively.
Approach
Define a recursive DFS that carries the necessary state.
Combine child results as the recursion unwinds.
Steps:
- Define a recursive DFS with state.
- Visit children and combine results.
- Return the final aggregation.
Example
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.
Python Solution
class Solution:
def numOfMinutes(
self, n: int, headID: int, manager: List[int], informTime: List[int]
) -> int:
def dfs(i: int) -> int:
ans = 0
for j in g[i]:
ans = max(ans, dfs(j) + informTime[i])
return ans
g = defaultdict(list)
for i, x in enumerate(manager):
g[x].append(i)
return dfs(headID)
Complexity
The time complexity is , and the space complexity is . The space complexity is .
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.