A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company 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 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 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:
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:
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:
1 <= n <= 105
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.
Solutions
Solution 1: DFS
Thinking
In a tree of managers, a message starts at the head; an edge costs that employee's inform time. \(n \le 10^5\) forbids summing a path per leaf. After building the child list, \(\textit{dfs}(i)\) is the time for \(i\) to inform its whole subtree: \(\textit{informTime}[i]\) plus the slowest \(\textit{dfs}(j)\) among direct reports. The answer is \(\textit{dfs}(\textit{headID})\).
We first build an adjacent list \(g\) according to the \(\textit{manager}\) array, where \(g[i]\) represents all direct subordinates of employee \(i\).
Next, we design a function \(\textit{dfs}(i)\), which means the time required for employee \(i\) to notify all his subordinates (including direct subordinates and indirect subordinates), and then the answer is \(\textit{dfs}(\textit{headID})\).
In function \(\textit{dfs}(i)\), we need to traverse all direct subordinates \(j\) of \(i\). For each subordinate, employee \(i\) needs to notify him, which takes \(\textit{informTime}[i]\) time, and his subordinates need to notify their subordinates, which takes \(\textit{dfs}(j)\) time. We take the maximum value of \(\textit{informTime}[i] + \textit{dfs}(j)\) as the return value of function \(\textit{dfs}(i)\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of employees.
The reporting lines form a tree rooted at the head, and an edge costs that employee's inform time. With \(n \le 10^5\), recursing from the head to score every subtree is too deep: a chain makes the call depth \(n\).
The time for employee \(i\) to inform a whole subtree is \(i\)'s own inform time plus the slowest direct report. A leaf has no reports, so that time is \(0\). The value depends only on results that are already known for the children.
An explicit stack starts at \(\textit{headID}\) and walks \((\textit{employee}, \textit{state})\) in postorder. On entry we push the exit marker and then the reports, and on exit we store the maximum of \(\textit{informTime}[i] + \textit{time}[j]\). The head's time is the answer.
We first build an adjacent list \(g\) according to the \(\textit{manager}\) array, where \(g[i]\) represents all direct subordinates of employee \(i\).
The time for employee \(i\) to inform the whole subtree is \(\textit{informTime}[i]\) plus the maximum time among the direct reports, or \(0\) when there are no reports. An explicit stack walks from \(\textit{headID}\) in postorder and writes that time when the node is left. The answer is the head's time.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the number of employees.