lc 690
作者:互联网
- 思路本质上就是广度优先搜索
- 每遇到一个新节点,需要将其下属放到数组中,不断的遍历这个数组,更新重要度即可
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
if (employees.size() == 0) {
return 0;
}
unordered_map<int, Employee*> m;
for (auto employee: employees) {
m[employee->id] = employee;
}
vector<int> total_subs = m[id]->subordinates;
int result = m[id]->importance;
for (int i = 0; i < total_subs.size(); i++) {
Employee* tmp = m[total_subs[i]];
// if this employee has subordinates, add into total_subs
if (0 != tmp->subordinates.size()) {
total_subs.insert(total_subs.end(), tmp->subordinates.begin(),
tmp->subordinates.end());
}
// add importance
result += tmp->importance;
}
return result;
}
};
标签:tmp,690,lc,subordinates,employee,total,id,subs 来源: https://blog.csdn.net/weixin_36149892/article/details/116329978