-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeImportance.java
More file actions
38 lines (34 loc) · 898 Bytes
/
Copy pathEmployeeImportance.java
File metadata and controls
38 lines (34 loc) · 898 Bytes
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
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
public class EmployeeImportance {
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
}
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
for(Employee em : employees)
map.put(em.id, em);
int sum = 0;
Queue<Employee> queue = new LinkedList<Employee>();
if(map.containsKey(id)){
queue.add(map.get(id));
while(!queue.isEmpty()){
Employee em = queue.poll();
sum += em.importance;
if(em.subordinates.size() > 0){
List<Integer> subordianates = em.subordinates;
for(int subId : subordianates)
queue.add(map.get(subId));
}
}
}
return sum;
}
public static void main(String[] args) {
}
}