forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployee_Importance.cpp
30 lines (30 loc) · 960 Bytes
/
Employee_Importance.cpp
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
28
29
30
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
int getImportance(int id, vector<Employee*> const& employees, unordered_map<int, int>& employeeMap) {
int totalImportance = employees[employeeMap[id]]->importance;
for(int subordinateId : employees[employeeMap[id]]->subordinates) {
totalImportance += getImportance(subordinateId, employees, employeeMap);
}
return totalImportance;
}
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, int> employeeMap;
for(int i = 0; i < employees.size(); i++) {
employeeMap[employees[i]->id] = i;
}
return getImportance(id, employees, employeeMap);
}
};