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;
}
};