-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path690.employee-importance.java
48 lines (39 loc) · 1.24 KB
/
690.employee-importance.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
class Solution {
HashMap<Integer, Employee> gf;
public int getImportance(List<Employee> employees, int id) {
if(employees.size() == 0)
return 0;
gf = new HashMap<Integer, Employee>();
for(Employee e: employees){
// if(!gf.containsKey(e.id))
// System.out.println(e);
// if(e != null)
gf.put(e.id, e);
// gf.get(e.id).add(e);
}
if(!gf.containsKey(id))
return 0;
Queue<Integer> q = new LinkedList<Integer>();
q.offer(id);
int res = gf.get(id).importance;
while(!q.isEmpty()){
int sz = q.size();
while(sz>0){
int empid = q.poll();
List<Integer> sub = gf.get(empid).subordinates;
if(sub == null){
sz--;
continue;
}
for(int emp: sub){
if(gf.containsKey(emp)){
res += gf.get(emp).importance;
q.offer(emp);
}
}
sz--;
}
}
return res;
}
}