LeetCode 1376. Time Needed to Inform All Employees

Question

A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company has is the one with headID.

Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it’s guaranteed that the subordination relationships have a tree structure.

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

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

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

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.

Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
Output: 1
Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
The tree structure of the employees in the company is shown.

Input: n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1]
Output: 21
Explanation: The head has id = 6. He will inform employee with id = 5 in 1 minute.
The employee with id = 5 will inform the employee with id = 4 in 2 minutes.
The employee with id = 4 will inform the employee with id = 3 in 3 minutes.
The employee with id = 3 will inform the employee with id = 2 in 4 minutes.
The employee with id = 2 will inform the employee with id = 1 in 5 minutes.
The employee with id = 1 will inform the employee with id = 0 in 6 minutes.
Needed time = 1 + 2 + 3 + 4 + 5 + 6 = 21.

Solution

这是一个典型Graph的题目,需要先构建关系,这样可以优化后面的过程。这个题需要找到最长的时间,进而可以通知所有的员工。所以,我们可以使用BFS的方法,进行每层搜索并且到达叶子节点(表明已经是最末端的员工了)的时候对比从开始到目前节点所耗用的时间。这个也有点类似于743. Network Delay Time,但是743每个一点可能有几条路选择。但是本题要求了每个人只能有一个manager,所以不会有多条路到达同一个点,只需要用BFS每层遍历即可。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// time: O(n) space:O(n)
class Node {
int id;
int time;

public Node (int id, int time) {
this.id = id;
this.time = time;
}
}
// 需要用hashmap先保存父子关系。不用保持leaf节点的值再取最大,直接每次poll出来的时候比较就行。
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
Queue<Node> queue = new LinkedList<>();
HashMap<Integer, List<Integer>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
map.putIfAbsent(manager[i], new ArrayList<>());
map.get(manager[i]).add(i);
}
queue.offer(new Node(headID, 0));
int res = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node node = queue.poll();
int id = node.id;
res = Math.max(res, node.time); // 这里直接比较得到最大。相当于每次都在操作。
List<Integer> nexts = map.getOrDefault(id, new ArrayList<>());
if (nexts != null) {
for (int next : nexts) {
queue.offer(new Node(next, node.time + informTime[id]));
}
}
}
}
return res;
}