---
# System prepended metadata

title: LC 1376. Time Needed to Inform All Employees
tags: [BFS, medium, leedcode, DFS, python]

---

# LC 1376. Time Needed to Inform All Employees

### [Problem link](https://leetcode.com/problems/time-needed-to-inform-all-employees/)

###### tags: `leedcode` `python` `medium` `BFS` `DFS`

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

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

The head of the company wants to inform all the company employees 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 <code>i-th</code> employee needs <code>informTime[i]</code> 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:** 

```
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee in the company.
```

**Example 2:** 
<img alt="" src="https://assets.leetcode.com/uploads/2020/02/27/graph.png" style="width: 404px; height: 174px;" />
```
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.
```

 **Constraints:** 

- <code>1 <= n <= 10<sup>5</sup></code>
- <code>0 <= headID < n</code>
- <code>manager.length == n</code>
- <code>0 <= manager[i] < n</code>
- <code>manager[headID] == -1</code>
- <code>informTime.length == n</code>
- <code>0 <= informTime[i] <= 1000</code>
- <code>informTime[i] == 0</code> if employee <code>i</code> has no subordinates.
- It is  **guaranteed**  that all the employees can be informed.


## Solution 1 - DFS
```python=
class Solution:
    def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
        graph = [[] for _ in range(n)]

        for idx, parent in enumerate(manager):
            if parent != -1:
                graph[parent].append(idx)

        q = deque([(headID, 0)])
        res = 0

        while q:
            cur, time = q.pop()
            time += informTime[cur]

            res = max(res, time)

            for child in graph[cur]:
                q.append((child, time))

        return res
```

## Solution 2 - BFS
```python=
class Solution:
    def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
        graph = [[] for _ in range(n)]

        for idx, parent in enumerate(manager):
            if parent != -1:
                graph[parent].append(idx)

        q = deque([(headID, 0)])
        res = 0

        while q:
            cur, time = q.popleft()
            time += informTime[cur]

            res = max(res, time)

            for child in graph[cur]:
                q.append((child, time))

        return res
```

>### Complexity
>h = max height of the tree.
>w = max weight of the tree.
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(h)             |
>| Solution 2  | O(n)            | O(w)             |


## Note
BFS, DFS 就差在
```python=
cur, time = q.popleft()
```
```python=
cur, time = q.pop()
```
這裡而已, 在deque中, 雖然官方文件說popleft跟pop都是O(1), 但pop其實會比popleft還要快一些.
