Skip to content

Commit b6e8a1f

Browse files
authored
Update Readme.md
1 parent e10d939 commit b6e8a1f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1+
1. My DFS C++ Soution
2+
```
3+
/*
4+
// Definition for Employee.
5+
class Employee {
6+
public:
7+
int id;
8+
int importance;
9+
vector<int> subordinates;
10+
};
11+
*/
12+
class Solution {
13+
private:
14+
unordered_map<int, Employee*> _id2emp;
15+
public:
16+
int getImportance(Employee* emp) {
17+
int sumImpt = emp->importance;
18+
for (auto sub : emp->subordinates)
19+
sumImpt += getImportance(_id2emp[sub]);
20+
return sumImpt;
21+
}
22+
23+
int getImportance(vector<Employee*> employees, int id) {
24+
for (auto emp : employees)
25+
{
26+
_id2emp[emp->id] = emp;
27+
}
28+
return getImportance(_id2emp[id]);
29+
}
30+
};
31+
```
132

33+
2. My BFS Python Solution
34+
35+
```
36+
"""
37+
# Employee info
38+
class Employee:
39+
def __init__(self, id, importance, subordinates):
40+
# It's the unique id of each node.
41+
# unique id of this employee
42+
self.id = id
43+
# the importance value of this employee
44+
self.importance = importance
45+
# the id of direct subordinates
46+
self.subordinates = subordinates
47+
"""
48+
class Solution:
49+
def getImportance(self, employees, id):
50+
"""
51+
:type employees: Employee
52+
:type id: int
53+
:rtype: int
54+
"""
55+
map = {}
56+
for e in employees:
57+
map[e.id] = e
58+
from collections import deque
59+
dq = deque()
60+
dq.append(map[id])
61+
visited = set()
62+
sumImp = 0
63+
while dq:
64+
e = dq.popleft()
65+
if e in visited:
66+
continue
67+
visited.add(e)
68+
sumImp += e.importance
69+
for sub in e.subordinates:
70+
dq.append(map[sub])
71+
return sumImp
72+
```

0 commit comments

Comments
 (0)