LeetCode 690. Employee Importance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> m;
for (Employee* e : employees)
m[e->id] = e;
function<int(int)> dfs = [&](int id) {
Employee* e = m[id];
int ans = e->importance;
for (int sub : e->subordinates)
ans += dfs(sub);
return ans;
};
return dfs(id);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> m;
for (const auto& e : employees)
m[e->id] = e;
int ans = 0;
queue<int> q;
q.push(id);
while (!q.empty()) {
int curr = q.front(); q.pop();
Employee* e = m[curr];
ans += e->importance;
for (int sub : e->subordinates)
q.push(sub);
}
return ans;
}
};