Employee Importance LeetCode solution in Java

1
class Solution {
2

3
private Map<Integer, Integer> idToIndex;
4

5
private void populateIdToIndexMap(List<Employee> employees) {
6
for (int idx = 0; idx < employees.size(); idx++) {
7
idToIndex.put(employees.get(idx).id, idx);
8
}
9
}
10

11
private int dfsGetImportance(List<Employee> employees, int id) {
12
int currEmpIdx = idToIndex.get(id);
13
Employee currEmp = employees.get(currEmpIdx);
14
int totalImportance = currEmp.importance;
15
for (int child : currEmp.subordinates) {
16
totalImportance += dfsGetImportance(employees, child);
17
}
18

19
return totalImportance;
20
}
21

22
public int getImportance(List<Employee> employees, int id) {
23
if (employees == null || employees.size() < 1) {
24
return 0;
25
}
26

27
idToIndex = new HashMap<>();
28
populateIdToIndexMap(employees);
29
return dfsGetImportance(employees, id);
30
}
31
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0