Employee Importance

Medium Graphs BFS / DFS Basics Original on LeetCode

Reported atAtlassian

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 <= 2000
  • 1 <= 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.
  • id is 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.

00:00
Testcase

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.

ApproachTimeSpace
DFS with linear lookupO(n²)O(n)
Optimal (hash map + BFS)O(n)O(n)

1DFS with linear lookup

TimeO(n²)Up to n employees are visited and each lookup scans up to n entries.
SpaceO(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.

  1. find(id): loop over employees until the id matches.
  2. total(id) = find(id).importance + sum of total(sub) for each subordinate.
  3. 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;
    }
}

2Optimal (hash map + BFS)

TimeO(n)Building the map is O(n) and each employee in the subtree is dequeued once.
SpaceO(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.

  1. byId = map id → employee.
  2. Queue the queried id; total = 0.
  3. Pop an id, add its importance, push all its subordinates.
  4. Return total when 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;
    }
}

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.