forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
24 lines (24 loc) · 815 Bytes
/
s1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// OJ: https://leetcode.com/problems/time-needed-to-inform-all-employees/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
unordered_map<int, vector<int>> m; // manager => subordinates
for (int i = 0; i < n; ++i) m[manager[i]].push_back(i);
queue<pair<int, int>> q;
int ans = 0;
q.emplace(headID, informTime[headID]);
while (q.size()) {
int cnt = q.size();
while (cnt--) {
auto p = q.front();
q.pop();
ans = max(ans, p.second);
for (int sub : m[p.first]) q.push({ sub, p.second + informTime[sub] });
}
}
return ans;
}
};