Employee Importance
// 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 subordinates;
};
*/
class Solution {
public:
//id员工的下属员工价值和
int getImportance(vector<Employee*> employees, int id) {
//构建employee的hash表 id -> employee
Employee* hash[2001];
//if(employees[id] == NULL) return 0;
for(int i = 0; i < employees.size(); i ++) {
//cout<<employees[i]->id<<endl;
hash[employees[i]->id] = employees[i];
}
//cout<<employees[id]->subordinates.size()<<endl;
if(hash[id]->subordinates.size() == 0) return hash[id]->importance;
int total = hash[id]->importance;
//对其直接下属dfs 求importance和
for(int i = 0; i < hash[id]->subordinates.size(); i ++) {
total += getImportance(employees, hash[id]->subordinates[i]);
}
return total;
}
};