[LeetCode] Employee Importance

690. Employee Importance

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 where:

  • employees[i].id is the ID of the ith employee.
  • employees[i].importance is the importance value of the ith employee.
  • employees[i].subordinates is a list of the IDs of the direct subordinates of the ith employee.

Given an integer id that represents an employee’s ID, return the total importance value of this employee and all their direct and indirect subordinates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/

class Solution {
unordered_map<int, Employee*> employeeIdMap;
int res = INT_MIN;
int getTotalImportance(Employee* employee) {
int importance = employee->importance;
for(auto& sub : employee->subordinates) {
importance += getTotalImportance(employeeIdMap[sub]);
}
return importance;
}
public:
int getImportance(vector<Employee*> employees, int id) {
for(auto& e : employees)
employeeIdMap[e->id] = e;
return getTotalImportance(employeeIdMap[id]);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/13/PS/LeetCode/employee-importance/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.