-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathEmployee Importance.java
31 lines (25 loc) · 977 Bytes
/
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
class Solution {
private Map<Integer, Integer> idToIndex;
private void populateIdToIndexMap(List<Employee> employees) {
for(int idx = 0; idx<employees.size(); idx++) {
idToIndex.put(employees.get(idx).id, idx);
}
}
private int dfsGetImportance(List<Employee> employees, int id) {
int currEmpIdx = idToIndex.get(id);
Employee currEmp = employees.get(currEmpIdx);
int totalImportance = currEmp.importance;
for(int child : currEmp.subordinates) {
totalImportance += dfsGetImportance(employees, child);
}
return totalImportance;
}
public int getImportance(List<Employee> employees, int id) {
if(employees == null || employees.size() < 1) {
return 0;
}
idToIndex = new HashMap<>();
populateIdToIndexMap(employees);
return dfsGetImportance(employees, id);
}
}