-
Notifications
You must be signed in to change notification settings - Fork 0
/
1376.time-needed-to-inform-all-employees.java
58 lines (47 loc) · 1.7 KB
/
1376.time-needed-to-inform-all-employees.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
HashMap<Integer, List<Integer>> grp;
int res;
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
Queue<Integer> q = new LinkedList<Integer>();
int[] ress = new int[n];
grp = new HashMap<Integer, List<Integer>>();
for(int i=0;i<manager.length;i++){
if(manager[i] == -1)
continue;
if(!grp.containsKey(manager[i]))
grp.put(manager[i], new ArrayList<Integer>());
grp.get(manager[i]).add(i);
}
// res = 0;
q.offer(headID);
while(!q.isEmpty()){
int sz = q.size();
int currmax = 0;
while(sz>0){
int curnum = q.poll();
if(curnum == -1){
sz--;
continue;
}
List<Integer> currinformrs = grp.get(curnum);
if(currinformrs == null){
sz--;
continue;
}
for(int i=0;i<currinformrs.size();i++){
ress[currinformrs.get(i)] = ress[curnum]+informTime[currinformrs.get(i)];
if(grp.containsKey(currinformrs.get(i))){
q.offer(currinformrs.get(i));
}
}
sz--;
}
}
res = 0;
for(int i=0;i<n;i++)
res = informTime[i] == 0 ? Math.max(res, ress[i]) : res;
return res+informTime[headID];
}
// public void solve(int i, int[] info, int sum){
// }
}