-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_690_EmployeeImportance.cpp
63 lines (47 loc) · 1.35 KB
/
LC_690_EmployeeImportance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
690. Employee Importance
https://leetcode.com/problems/employee-importance/
*/
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
class Solution {
public:
unordered_map<int,int> umap;
int impSum=0;
void dfsHelper(vector<Employee*> &employees, int id)
{
int i = umap[id];
impSum += employees[i]->importance;
for(int subid: employees[i]->subordinates) // all subId are valid.
dfsHelper(employees, subid);
}
void bfsHelper(vector<Employee*> &employees, int id)
{
queue<int> q;
q.push(id);
int i;
while(!q.empty())
{
i = umap[q.front()]; q.pop();
impSum += employees[i]->importance;
for(int subid: employees[i]->subordinates) // all subId are valid.
{
q.push(subid);
}
}
}
int getImportance(vector<Employee*> employees, int id) {
for(int i = 0; i < employees.size(); i++) // unique emp id
umap[employees[i]->id] = i; // storing in unordered map empid --> i value
// dfsHelper(employees, id);
bfsHelper(employees, id);
return impSum;
}// end
};