Time Needed to Inform All Employees - Problem

A company has n employees with unique IDs from 0 to n - 1. The head of the company has ID headID.

Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, and manager[headID] = -1. The subordination relationships form a tree structure.

The head wants to inform all employees of urgent news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know the news.

The i-th employee needs informTime[i] minutes to inform all direct subordinates. After informTime[i] minutes, all direct subordinates can start spreading the news.

Return the number of minutes needed to inform all employees about the urgent news.

Input & Output

Example 1 — Basic Tree Structure
$ Input: n = 6, headID = 0, manager = [-1,0,0,1,1,2], informTime = [1,1,0,0,0,0]
Output: 2
💡 Note: Employee 0 needs 1 minute to inform employees 1 and 2. Employee 1 needs 1 minute to inform employees 3 and 4. The maximum time is 1 + 1 = 2 minutes to reach employees 3 and 4.
Example 2 — Single Path
$ Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
💡 Note: Only one employee (the head), so no time needed to inform anyone.
Example 3 — Unbalanced Tree
$ Input: n = 4, headID = 0, manager = [-1,0,1,2], informTime = [2,1,0,0]
Output: 3
💡 Note: Chain: 0 → 1 → 2 → 3. Total time: 2 + 1 + 0 = 3 minutes to reach employee 3.

Constraints

  • 1 ≤ n ≤ 105
  • 0 ≤ headID < n
  • manager.length == n
  • 0 ≤ manager[i] < n
  • manager[headID] == -1
  • informTime.length == n
  • 0 ≤ informTime[i] ≤ 1000
  • informTime[i] == 0 if employee i has no subordinates

Visualization

Tap to expand
Time Needed to Inform All Employees INPUT Employee Hierarchy Tree 0 t=1 1 t=1 2 t=0 3 t=0 4 t=0 5 t=0 n = 6, headID = 0 manager: [-1,0,0,1,1,2] informTime: [1,1,0,0,0,0] t = informTime (minutes) Head Manager Leaf ALGORITHM STEPS DFS Tree Traversal 1 Build Adjacency List Map each manager to their subordinates 2 DFS from Head (0) Traverse tree recursively tracking time at each node 3 Calculate Path Times Add informTime[node] to current path time 4 Find Maximum Path Return max time among all leaf nodes DFS Paths: 0-->1-->3: 1+1+0 = 2 0-->1-->4: 1+1+0 = 2 0-->2-->5: 1+0+0 = 1 Max = 2 FINAL RESULT Time Flow Visualization t=0 0 t=1 1 2 t=2 3 4 5 Output: 2 2 minutes needed to inform all employees OK - Verified Key Insight: The total time to inform all employees equals the MAXIMUM path sum from root to any leaf node. Information spreads in parallel to all subordinates, so we only care about the longest path (bottleneck). DFS traversal accumulates informTime values along each root-to-leaf path, returning the maximum. TutorialsPoint - Time Needed to Inform All Employees | DFS Tree Traversal Approach Time Complexity: O(n) | Space Complexity: O(n)
Asked in
Amazon 25 Microsoft 18 Google 15 Facebook 12
125.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen