题目:690. 员工的重要性
思路:先用哈希表map将每个员工的信息映射到员工ID对应的下标位置。接着使用深度优先搜索dfs,来记录总和即可。细节看注释
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
class Solution {
public:
//哈希表
unordered_map<int,Employee*> mp;
int dfs(int id){
Employee * tmp=mp[id];
int sum=tmp->importance;
for(auto x:tmp->subordinates){
sum+=dfs(x);
}
return sum;
}
int getImportance(vector<Employee*> employees, int id) {
//将每个员工的信息映射到员工ID对应的下标位置
for(auto x:employees){
mp[x->id]=x;
}
//深度优先搜索dfs
return dfs(id);
}
};
标签:690,int,dfs,员工,哈希,Employee,id
From: https://blog.csdn.net/weixin_46028214/article/details/141557383