0% completed
Problem Statement
A company has n
employees, each with a unique ID
from 0
to n-1
. The head of the company has an ID headID
.
Each employee has a direct manager, indicated by the manager
array, where manager[i]
is the direct manager of the i<sup>th</sup> employee. The head of the company has no manager, so manager[headID] = -1
.
The company needs to inform all employees about urgent news. The head will inform their direct subordinates, who will then inform their subordinates, and so on. Each employee i
needs informTime[i]
minutes to inform all their direct subordinates.
Return the number of minutes
needed to inform all the employees about the urgent news.
Examples
Example 1
- Input: n:
6
, headID:2
, manager:[2, 2, -1, 2, 2, 2]
, informTime:[0, 0, 1, 0, 0, 0]
- Output:
1
- Explanation: Employee
2
informs all subordinates (0, 1, 3, 4, 5
) in1
minute.
Example 2
- Input: n:
4
, headID:0
, manager:[-1, 0, 0, 1]
, informTime:[2, 1, 0, 0]
- Output:
3
- Explanation:
- Employee
0
informs1
and2
in2
minutes. - Employee
1
informs3
in1
more minute. Total time =2 + 1 = 3
minutes.
- Employee
Example 3
- Input: n:
7
, headID:6
, manager:[1, 3, 3, 4, 6, 6, -1]
, informTime:[0, 2, 0, 1, 2, 0, 1]
- Output:
6
- Explanation: Employee
6
informs4
and5
in1
minute,4
informs3
in2
minute,3
informs1
and2
in1
minute,and1
informs0
in2
minutes. Total time =1 + 2 + 1 + 2
=6
minutes.
Constraints:
- 1 <= n <= 10<sup>5</sup>
- 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.
- It is guaranteed that all the employees can be informed.
Try it yourself
Try solving this question here:
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible