LeetCode 690. 员工的重要性
原题链接
简单
作者:
飞呀
,
2021-05-02 00:22:01
,
所有人可见
,
阅读 301
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int n = employees.size();
map<int, Employee*> map;
for(int i = 0; i < n; i++){
map[employees[i]->id] = employees[i];
}
int sum = 0;
queue<Employee*> q;
q.push(map[id]);
Employee* tmp;
while(!q.empty()){
tmp = q.front();
q.pop();
sum += tmp->importance;
vector<int> sub = tmp->subordinates;
for(int i = 0; i < sub.size(); i++){
q.push(map[sub[i]]);
}
}
return sum;
}
};