Employee Importance
Thoughts
Code
/*
// 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 employee : employees) {
map.put(employee.id, employee);
}
int res = 0;
Queue<Employee> queue = new LinkedList<>();
queue.offer(map.get(id));
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Employee employee = queue.poll();
res += employee.importance;
for (Integer sub : employee.subordinates) {
queue.offer(map.get(sub));
}
}
}
return res;
}
}Analysis
Last updated