-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgetImportance.java
34 lines (30 loc) · 940 Bytes
/
getImportance.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
/*
// 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) {
int importance = 0;
HashMap<Integer, Employee> map = new HashMap<>();
for(Employee employee : employees)
map.put(employee.id, employee);
Queue<Employee> queue = new LinkedList<>();
queue.offer(map.get(id));
while(!queue.isEmpty())
{
Employee node = queue.poll();
importance += node.importance;
for(int subor: node.subordinates)
queue.offer(map.get(subor));
}
return importance;
}
}