forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
21 lines (21 loc) · 663 Bytes
/
s2.cpp
File metadata and controls
21 lines (21 loc) · 663 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// OJ: https://leetcode.com/problems/time-needed-to-inform-all-employees/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
vector<vector<int>> m; // manager => subordinates
int dfs(int id, vector<int>& time) {
int ans = 0;
for (int n : m[id]) ans = max(ans, dfs(n, time));
return time[id] + ans;
}
public:
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
m.resize(n);
for (int i = 0; i < n; ++i) {
if (manager[i] == -1) continue;
m[manager[i]].push_back(i);
}
return dfs(headID, informTime);
}
};