Forgetting null/base-case handling
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.
Move from brute-force thinking to an efficient approach using tree strategy.
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.
Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure.
The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
Return the number of minutes needed to inform all the employees about the urgent news.
Example 1:
Input: n = 1, headID = 0, manager = [-1], informTime = [0] Output: 0 Explanation: The head of the company is the only employee in the company.
Example 2:
Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0] Output: 1 Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all. The tree structure of the employees in the company is shown.
Constraints:
1 <= n <= 1050 <= headID < nmanager.length == n0 <= manager[i] < nmanager[headID] == -1informTime.length == n0 <= informTime[i] <= 1000informTime[i] == 0 if employee i has no subordinates.Problem summary: 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. Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordination relationships have a tree structure. The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news. The i-th employee needs informTime[i] minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news). Return the number of minutes needed to inform all the employees about the urgent news.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Tree
1 0 [-1] [0]
6 2 [2,2,-1,2,2,2] [0,0,1,0,0,0]
maximum-depth-of-binary-tree)binary-tree-maximum-path-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1376: Time Needed to Inform All Employees
class Solution {
private List<Integer>[] g;
private int[] informTime;
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
this.informTime = informTime;
for (int i = 0; i < n; ++i) {
if (manager[i] >= 0) {
g[manager[i]].add(i);
}
}
return dfs(headID);
}
private int dfs(int i) {
int ans = 0;
for (int j : g[i]) {
ans = Math.max(ans, dfs(j) + informTime[i]);
}
return ans;
}
}
// Accepted solution for LeetCode #1376: Time Needed to Inform All Employees
func numOfMinutes(n int, headID int, manager []int, informTime []int) int {
g := make([][]int, n)
for i, x := range manager {
if x != -1 {
g[x] = append(g[x], i)
}
}
var dfs func(int) int
dfs = func(i int) (ans int) {
for _, j := range g[i] {
ans = max(ans, dfs(j)+informTime[i])
}
return
}
return dfs(headID)
}
# Accepted solution for LeetCode #1376: Time Needed to Inform All Employees
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)
// Accepted solution for LeetCode #1376: Time Needed to Inform All Employees
struct Solution;
impl Solution {
fn num_of_minutes(n: i32, _: i32, mut manager: Vec<i32>, mut inform_time: Vec<i32>) -> i32 {
let n = n as usize;
let mut res = 0;
for i in 0..n {
res = res.max(Self::dfs(i, &mut manager, &mut inform_time));
}
res
}
fn dfs(i: usize, manager: &mut Vec<i32>, inform_time: &mut Vec<i32>) -> i32 {
if manager[i] != -1 {
inform_time[i] += Self::dfs(manager[i] as usize, manager, inform_time);
manager[i] = -1;
}
inform_time[i]
}
}
#[test]
fn test() {
let n = 1;
let head_id = 0;
let manager = vec![-1];
let inform_time = vec![0];
let res = 0;
assert_eq!(
Solution::num_of_minutes(n, head_id, manager, inform_time),
res
);
let n = 6;
let head_id = 2;
let manager = vec![2, 2, -1, 2, 2, 2];
let inform_time = vec![0, 0, 1, 0, 0, 0];
let res = 1;
assert_eq!(
Solution::num_of_minutes(n, head_id, manager, inform_time),
res
);
let n = 7;
let head_id = 6;
let manager = vec![1, 2, 3, 4, 5, 6, -1];
let inform_time = vec![0, 6, 5, 4, 3, 2, 1];
let res = 21;
assert_eq!(
Solution::num_of_minutes(n, head_id, manager, inform_time),
res
);
let n = 15;
let head_id = 0;
let manager = vec![-1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6];
let inform_time = vec![1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0];
let res = 3;
assert_eq!(
Solution::num_of_minutes(n, head_id, manager, inform_time),
res
);
let n = 4;
let head_id = 2;
let manager = vec![3, 3, -1, 2];
let inform_time = vec![0, 0, 162, 914];
let res = 1076;
assert_eq!(
Solution::num_of_minutes(n, head_id, manager, inform_time),
res
);
}
// Accepted solution for LeetCode #1376: Time Needed to Inform All Employees
function numOfMinutes(n: number, headID: number, manager: number[], informTime: number[]): number {
const g: number[][] = new Array(n).fill(0).map(() => []);
for (let i = 0; i < n; ++i) {
if (manager[i] !== -1) {
g[manager[i]].push(i);
}
}
const dfs = (i: number): number => {
let ans = 0;
for (const j of g[i]) {
ans = Math.max(ans, dfs(j) + informTime[i]);
}
return ans;
};
return dfs(headID);
}
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.