690. Employee Importance
Last Updated: 2020.06.09
Question Source: https://leetcode.com/problems/employee-importance
# BFS
class Solution:
def getImportance(self, employees, id):
dic = {}
# construct dictionary
for employee in employees:
adjList[employee.id] = employee
q = deque()
q.append(id)
ans = 0
while q:
first_e = q.popleft()
if first_e not in dic: return 'error'
ans += dic[first_e].importance
for sub in dic[first_e].subordinates:
q.append(sub)
return res
# DFS
class Solution:
def getImportance(self, employees, id):
adjList = {}
# construct dictionary
for employee in employees:
adjList[employee.id] = employee
# look up employee by id
return self._dfs(adjList, id)
def _dfs(self, adjList, id):
ans = adjList[id].importance
for sub in adjList[id].subordinates: # int
ans += self._dfs(adjList, sub)
return ans