Employee Importance LeetCode solution in C++

1
class Solution {
2
public:
3
int ans = 0;
4
void helper(unordered_map<int, pair<int, vector<int>>> &res, int id, vector<int> &dp) {
5
if (dp[id] == -1) {
6
ans += res[id].first;
7
dp[id] = 1;
8
} // employees[id-1]->importance;dp[id-1]=1;}
9
else
10
return;
11
for (int i = 0; i < res[id].second.size(); i++) helper(res, res[id].second[i], dp);
12
}
13
int getImportance(vector<Employee *> employees, int id) {
14
vector<int> dp(2001, -1);
15
unordered_map<int, pair<int, vector<int>>> res;
16
for (int i = 0; i < employees.size(); i++)
17
res[employees[i]->id] = {employees[i]->importance, employees[i]->subordinates};
18
helper(res, id, dp);
19
return ans;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0