0690. Employee Importance
https://leetcode.com/problems/employee-importance
Description
You have a data structure of employee information, which includes the employee's unique id, their importance value, and their direct subordinates' id.
You are given an array of employees employees where:
employees[i].idis the ID of theithemployee.employees[i].importanceis the importance value of theithemployee.employees[i].subordinatesis a list of the IDs of the subordinates of theithemployee.
Given an integer id that represents the ID of an employee, return the total importance value of this employee and all their subordinates.
Example 1:

**Input:** employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1
**Output:** 11
**Explanation:** Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3.
They both have importance value 3.
So the total importance value of employee 1 is 5 + 3 + 3 = 11.Example 2:

**Input:** employees = [[1,2,[5]],[5,-3,[]]], id = 5
**Output:** -3Constraints:
1 <= employees.length <= 20001 <= employees[i].id <= 2000All
employees[i].idare unique.-100 <= employees[i].importance <= 100One employee has at most one direct leader and may have several subordinates.
idis guaranteed to be a valid employee id.
ac
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<>();
for (Employee e : employees) {
map.put(e.id, e);
}
return map.get(id).importance + sum(map.get(id).subordinates, map);
}
public int sum(List<Integer> sub, Map<Integer, Employee> map) {
int res = 0;
for (Integer i : sub) {
res += map.get(i).importance;
res += sum(map.get(i).subordinates, map);
}
return res;
}
}
/*
1) put information into map; 2) recursively get subordinates importance
*/Last updated
Was this helpful?