Employee Importance
Employee Importance is a medium graphs problem solved with the bfs / dfs basics pattern.
The best approach, optimal (hash map + bfs), runs in O(n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from dfs with linear lookup up.
Problem
A company stores its staff as a list of Employee objects. Each one has an id, an integer importance, and subordinates: the ids of the people who report directly to them.
Given the list and one employee’s id, return the total importance of that employee plus everyone who reports to them directly or indirectly (their whole team below them, at every level).
In the examples each employee is written as [id, importance, [subordinate ids]].
Examples
Example 1
- Input
employees = [[4, 6, [7, 9]], [7, 2, [11]], [9, -1, []], [11, 3, []]], id = 4- Output
10- Why
- Employee 4 (6) leads 7 (2) and 9 (-1); 7 leads 11 (3). 6 + 2 - 1 + 3 = 10.
Example 2
- Input
employees = [[4, 6, [7, 9]], [7, 2, [11]], [9, -1, []], [11, 3, []]], id = 7- Output
5- Why
- Only employee 7 and the people under 7 count: 2 + 3.
Example 3
- Input
employees = [[1, -4, []], [2, 8, [1]]], id = 1- Output
-4- Why
- Employee 1 has nobody below them, so the answer is just their own value.
Constraints
1 <= employees.length <= 20001 <= employees[i].id <= 2000, all ids are distinct.-100 <= employees[i].importance <= 100- Each employee has at most one direct leader, and the reporting lines contain no cycles.
idis the id of one of the employees.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| DFS with linear lookup | O(n²) | O(n) |
| Optimal (hash map + BFS) | O(n) | O(n) |
1DFS with linear lookup
O(n²)Up to n employees are visited and each lookup scans up to n entries.O(n)The recursion can be n levels deep on a single chain.Walk the subtree recursively, but find each employee by scanning the whole list for the matching id.
- find(id): loop over
employeesuntil the id matches. - total(id) = find(id).importance + sum of total(sub) for each subordinate.
- Return total(id).
class Solution {
public int getImportance(List<Employee> employees, int id) {
Employee e = null;
for (Employee x : employees) if (x.id == id) e = x;
int total = e.importance;
for (int sub : e.subordinates) total += getImportance(employees, sub);
return total;
}
}class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
e = None
for x in employees:
if x.id == id:
e = x
total = e.importance
for sub in e.subordinates:
total += self.getImportance(employees, sub)
return totalclass Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
return total(employees, id);
}
private:
int total(vector<Employee*>& employees, int id) {
Employee* e = nullptr;
for (Employee* x : employees) if (x->id == id) e = x;
int sum = e->importance;
for (int sub : e->subordinates) sum += total(employees, sub);
return sum;
}
};function getImportance(employees, id) {
let e = null;
for (const x of employees) if (x.id === id) e = x;
let total = e.importance;
for (const sub of e.subordinates) total += getImportance(employees, sub);
return total;
}2Optimal (hash map + BFS)
O(n)Building the map is O(n) and each employee in the subtree is dequeued once.O(n)The map holds n entries and the queue at most n ids.Index every employee by id once, then run a breadth-first walk from the queried id, adding each visited employee's importance and queueing their subordinates. Every employee is touched once.
byId= map id → employee.- Queue the queried id;
total = 0. - Pop an id, add its importance, push all its subordinates.
- Return
totalwhen the queue is empty.
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> byId = new HashMap<>();
for (Employee e : employees) byId.put(e.id, e);
Deque<Integer> queue = new ArrayDeque<>();
queue.add(id);
int total = 0;
while (!queue.isEmpty()) {
Employee e = byId.get(queue.poll());
total += e.importance;
queue.addAll(e.subordinates);
}
return total;
}
}class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
by_id = {e.id: e for e in employees}
queue = deque([id])
total = 0
while queue:
e = by_id[queue.popleft()]
total += e.importance
queue.extend(e.subordinates)
return totalclass Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> byId;
for (Employee* e : employees) byId[e->id] = e;
queue<int> q;
q.push(id);
int total = 0;
while (!q.empty()) {
Employee* e = byId[q.front()];
q.pop();
total += e->importance;
for (int sub : e->subordinates) q.push(sub);
}
return total;
}
};function getImportance(employees, id) {
const byId = new Map();
for (const e of employees) byId.set(e.id, e);
const queue = [id];
let total = 0;
for (let head = 0; head < queue.length; head++) {
const e = byId.get(queue[head]);
total += e.importance;
for (const sub of e.subordinates) queue.push(sub);
}
return total;
}Edge cases to test
- The employee has no subordinates
- Negative importance values
- A long single chain of command (deep recursion)
- The list is not sorted by id, and the queried employee is the top leader
Hints
Hint 1
The reporting lines form a tree rooted at the queried employee. You need the sum of importance over that subtree.
Hint 2
Looking up an employee by id inside a list is slow. Build a map from id to employee first.
Hint 3
Then a DFS or BFS from the queried id adds up every importance it reaches.
FAQ
What is the best time complexity for Employee Importance?
Optimal (hash map + BFS) runs in O(n) time and O(n) extra space. Building the map is O(n) and each employee in the subtree is dequeued once.
Which pattern does Employee Importance use?
It is a graphs problem that uses the bfs / dfs basics pattern. Other problems with the same pattern: BFS - Normal + Level by Level, DFS - Recursive + Iterative, Clone Graph.
Is there a brute force solution for Employee Importance?
Yes. DFS with linear lookup takes O(n²) time and O(n) space. Walk the subtree recursively, but find each employee by scanning the whole list for the matching id.
Which edge cases should I test for Employee Importance?
The employee has no subordinates; Negative importance values; A long single chain of command (deep recursion); The list is not sorted by id, and the queried employee is the top leader.