A company has n employees with unique IDs from 0 to n - 1. The head of the company is the employee with ID head_id.
Each employee has one direct manager, given in the array manager, where manager[i] is the direct manager of employee i and manager[head_id] = -1. The reporting relationships are guaranteed to form a tree.
The head wants to tell every employee an urgent piece of news. The head informs their direct subordinates, those subordinates inform theirs, and so on until everyone knows.
Employee i needs inform_time[i] minutes to inform all of their direct subordinates. After inform_time[i] minutes, all of those subordinates know the news and can start spreading it.
Return the number of minutes needed for all employees to learn the news.
Function Signature
def num_of_minutes(n: int, head_id: int, manager: list[int], inform_time: list[int]) -> int:
Rules
-
manager[i]
is the direct manager of employee
i
. The head is the only employee with
manager[i] == -1
, and that employee is
head_id
.
-
The head knows the news at minute 0. If employee
i
learns it at minute
t
, every direct subordinate of
i
learns it at minute
t + inform_time[i]
. All managers spread the news in parallel.
-
Return the minute at which the last employee learns the news. For a company with one employee, that is 0.
Constraints
-
1 <= n <= 10^5
-
manager.length == n
-
inform_time.length == n
-
0 <= head_id < n
-
0 <= manager[i] < n
, or
manager[i] == -1
for the head
-
Exactly one employee has
manager[i] == -1
-
0 <= inform_time[i] <= 1000
-
inform_time[i] == 0
if employee
i
has no subordinates
-
The relationships form a valid tree.
Examples
Input: n = 1, head_id = 0, manager = [-1], inform_time = [0]
Output: 0
The only employee is the head, so nobody else needs to be told.
Input: n = 6, head_id = 2, manager = [2, 2, -1, 2, 2, 2], inform_time = [0, 0, 1, 0, 0, 0]
Output: 1
Org chart:
2 (head, inform_time = 1)
/ | | | \
0 1 3 4 5
Employee 2 is the head and manages everyone else directly. All five subordinates learn the news together after inform_time[2] = 1 minute.
Input: n = 7, head_id = 0, manager = [-1, 0, 0, 1, 1, 2, 2], inform_time = [1, 2, 3, 0, 0, 0, 0]
Output: 4
Employees 1 and 2 learn at minute 1. Employee 1 then tells 3 and 4, who know at minute 1 + 2 = 3, and employee 2 tells 5 and 6, who know at minute 1 + 3 = 4. The last employees to learn are 5 and 6, at minute 4.