Employee Importance LeetCode solution in Python

1
# Runtime: 254 ms (Top 40.58%) | Memory: 15.6 MB (Top 60.31%)
2
"""
3
# Definition for Employee.
4
class Employee:
5
def __init__(self, id: int, importance: int, subordinates: List[int]):
6
self.id = id
7
self.importance = importance
8
self.subordinates = subordinates
9
"""
10

11

12
class Solution:
13
def dfs(self, graph, employees, empId, totalImportance, visited):
14
totalImportance += graph[empId][0]
15
visited.add(empId)
16

17
for neighbour in graph[empId][1]:
18
if neighbour not in visited:
19
totalImportance = self.dfs(
20
graph, employees, neighbour, totalImportance, visited
21
)
22

23
return totalImportance
24

25
def getImportance(self, employees: List["Employee"], id: int) -> int:
26
graph = {}
27

28
for employeeInfo in employees:
29
empId, importance, suboordinates = (
30
employeeInfo.id,
31
employeeInfo.importance,
32
employeeInfo.subordinates,
33
)
34
graph[empId] = [importance, suboordinates]
35

36
n = len(employees)
37

38
if graph[id][1] == []:
39
return graph[id][0]
40

41
visited = set()
42
totalImportance = 0
43
totalImportance = self.dfs(graph, employees, id, totalImportance, visited)
44

45
return totalImportance

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0