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
Company Information Flow: Time to Inform All EmployeesInput:manager = [-1,0,0,1,1,2]informTime = [1,1,0,0,0,0]0Head: 1 min11 min20 min3leaf4leaf5leafInformation Flow Timing:t=0: Head (0) starts informingt=1: Employees 1,2 get informedt=2: Employees 3,4 get informed (MAX)t=1: Employee 5 gets informedAnswer: 2 minutesMaximum time to reach any employee
Understanding the Visualization
1
Input Tree
Company hierarchy with manager relationships and inform times
2
Information Flow
Information spreads from head through managers to employees
3
Maximum Time
Find the longest path from head to any employee
Key Takeaway
🎯 Key Insight: The problem is finding the maximum depth-weighted path in a tree, where each node contributes its inform time to the total.
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