-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee-importance.py
30 lines (27 loc) · 983 Bytes
/
employee-importance.py
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
"""
# Definition for Employee.
class Employee:
def __init__(self, id: int, importance: int, subordinates: List[int]):
self.id = id
self.importance = importance
self.subordinates = subordinates
"""
class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
importance_dict = defaultdict()
adjacency_list = defaultdict(list)
for e in employees:
adjacency_list[e.id].extend(e.subordinates)
importance_dict[e.id] = e.importance
# for key in adjacency_list.keys():
# print(key, adjacency_list[key])
visited = set()
self.count_importance = 0
def dfs(node):
visited.add(node)
self.count_importance += importance_dict[node]
for neighbor in adjacency_list[node]:
if neighbor not in visited:
dfs(neighbor)
dfs(id)
return self.count_importance