-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee Importance.java
36 lines (33 loc) · 1.01 KB
/
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
/*
// Employee info
class Employee {
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public List<Integer> subordinates;
};
*/
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<>();
for(Employee e: employees) map.put(e.id, e);
Set<Integer> visited = new HashSet<>();
Queue<Integer> q = new LinkedList<>();
q.offer(id);
int res = 0;
while(!q.isEmpty()) {
Employee e = map.get(q.poll());
visited.add(e.id);
res += e.importance;
List<Integer> sub = e.subordinates;
if(sub == null) continue;
for(int i: sub) {
if(!visited.contains(i)) q.offer(i);
}
}
return res;
}
}