Time Needed to Inform All Employees

A company has n employees with unique ids 0 to n - 1, and headID is the head of the company. manager[i] is the direct manager of employee i (manager[headID] = -1), and the subordination forms a tree. The head wants to share urgent news. Employee i needs informTime[i] minutes to inform all of their direct subordinates, after which those subordinates start spreading it further. Return the number of minutes needed to inform all employees.

30:00

Time Needed to Inform All Employees
medium
Free written solution
Topics
Companies

A company has n employees with unique ids 0 to n - 1, and headID is the head of the company. manager[i] is the direct manager of employee i (manager[headID] = -1), and the subordination forms a tree. The head wants to share urgent news. Employee i needs informTime[i] minutes to inform all of their direct subordinates, after which those subordinates start spreading it further. Return the number of minutes needed to inform all employees.

Example 1:
Input: {"n":6,"headID":2,"manager":[2,2,-1,2,2,2],"informTime":[0,0,1,0,0,0]}
Output: 1
Constraints:
  • 1≤n≤1051 \leq n \leq 10^5

  • 0≤headID<n0 \leq \text{headID} < n

  • manager.length=n\text{manager.length} = n, manager[headID]=−1\text{manager[headID]} = -1

  • informTime.length=n\text{informTime.length} = n, 0≤informTime[i]≤10000 \leq \text{informTime}[i] \leq 1000

  • informTime[i]=0\text{informTime}[i] = 0 if employee ii has no subordinates

  • All employees are guaranteed to be reachable from the head.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":6,"headID":2,"manager":[2,2,-1,2,2,2],"informTime":[0,0,1,0,0,0]}

Turn the manager array into a children list. Employee 2 is the head; 1 employee manages someone.

+1+1+1+1+10?1?2head?3?4?5?

○ manager being popped

○ just told

○ hears latest so far

+k = manager's inform time

queue (employee, minute) · 0

empty

best

0m

informed

0/6

Variables
VariableValue
n6
headID2
best0
boss-
queue0
DepthFunction Call
Stack empty
0/9