思路:找下属,求重要性和直接用BFS即可,关键是这里的数据结构不是链表,如何最快速度找到下属是最重要的;这里我用map将每个员工的id-下标索引
存储起来,直接通过id得到其员工属性;
int getImportance(vector<Employee *> employees, int id)
{
map<int, int> m; //建立员工id-数组索引的映射;
int len = employees.size();
//初始化m
for (int i = 0; i < len; i++)
{
m[employees[i]->id] = i;
}
int import_sum = 0;
queue<int> q;
q.push(id);
while (!q.empty())
{
int q_len = q.size();
while (q_len--)
{
Employee *cur = employees[m[q.front()]];
q.pop();
import_sum += cur->importance;
for (auto i : cur->subordinates)
{
q.push(i);
}
}
}
return import_sum;
}