Employee Importance
Calculate the total importance value of an employee and all their direct and indirect subordinates using DFS.
01The problem
You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.
You are given an array of employees employees and an integer id that represents an employee's ID. Return the total importance value of this employee and all their direct and indirect subordinates.
| Input | Output | Why |
|---|---|---|
employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], id = 1 | 11 | Leader 1 (imp 5) + subordinate 2 (imp 3) + subordinate 3 (imp 3) = 11. |
Constraints
1 <= employees.length <= 20000 <= employees[i].importance <= 100- All
employees[i].idare unique. - Each employee has unique subordinates.
02How to spot it
- "Subordinate hierarchy" / "Tree rooted at employee": The company hierarchy is a tree or forest.
- HashMap for
O(1)employee lookup: Convert the employee list into aMap<Integer, Employee>mappingid -> Employee, then run DFS from the targetid.
03Pseudocode
byId = map id -> employee
dfs(id):
e = byId[id]
total = e.importance
for sub in e.subordinates:
total += dfs(sub)
return total04Watch it work
subordinate tree (1 -> 2, 3)
status
start DFS at leader 1
Compute total importance of employee 1 and all direct/indirect subordinates.
Pseudocode
- byId = map id -> employee
- dfs(id):
- e = byId[id]
- total = e.importance
- for sub in e.subordinates:
- total += dfs(sub)
- return total
Variables
- targetId
- 1
05Step-by-step dry run
We trace id = 1:
| Step | Current Employee ID | Importance added | Subordinates | Total importance so far |
|---|---|---|---|---|
| 1 | 1 | 5 | [2, 3] | 5 |
| 2 | 2 | 3 | [] (leaf) | 5 + 3 = 8 |
| 3 | 3 | 3 | [] (leaf) | 8 + 3 = 11 |
Output: 11.
06Java solution
import java.util.*;
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
public Employee(int id, int importance, List<Integer> subordinates) {
this.id = id;
this.importance = importance;
this.subordinates = subordinates;
}
}
public class Solution {
public static int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> byId = new HashMap<>();
for (Employee e : employees) {
byId.put(e.id, e);
}
return dfs(byId, id);
}
private static int dfs(Map<Integer, Employee> byId, int id) {
Employee e = byId.get(id);
int total = e.importance;
for (int subId : e.subordinates) {
total += dfs(byId, subId);
}
return total;
}
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, 5, Arrays.asList(2, 3)),
new Employee(2, 3, Collections.emptyList()),
new Employee(3, 3, Collections.emptyList())
);
System.out.println(getImportance(employees, 1));
}
}11
07Why it works
The management structure is a directed acyclic tree. Mapping id -> Employee provides O(1) access to any employee record. The recursive DFS sums the root's importance and the returned importance of each subtree.
08Time and space
- Time complexity:
O(N)whereNis the number of employees in the company. In the worst case, all employees are in the subtree ofid. - Space complexity:
O(N)to store theHashMapand recursion call stack.
09Edge cases
- Employee has no subordinates: Loop doesn't execute; returns
e.importance.
10Common mistakes
- Searching the
List<Employee>linearly at every recursive call: Linear search takesO(N)per lookup, making total timeO(N^2). Precomputing theHashMapdrops lookup toO(1).
11In the interview
- Opening line: "I will preprocess the employee list into a HashMap for
O(1)ID lookup, then perform DFS starting from the target ID to sum the importance of all subordinates in the subtree."
12Quiz
Why is building a HashMap of id -> Employee essential before starting DFS?
- (Recommended) To achieve O(1) employee lookup during recursion instead of O(N) linear search
- Because employees cannot be traversed without maps
- To sort employees by importance
- To remove duplicate IDs
Answer: To achieve O(1) employee lookup during recursion instead of O(N) linear search.
13Solve it yourself
Open LeetCode #690 and submit the solution.