May——690.员工的重要性

May——690.员工的重要性

"""
# 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:
        mp = {employee.id: employee for employee in employees}
        print(mp)
        
        #DFS
        def dfs(id):
            employee = mp[id]
            total = employee.importance + sum(dfs(subIdx) for subIdx in employee.subordinates)
            return total
        
        return dfs(id)

        #BFS
        queue = deque([id])
        res = 0
        while queue:
            id = queue.popleft()
            employee = mp[id]
            res+=employee.importance
            for sub_id in employee.subordinates:
                queue.append(sub_id)
        
        return res

        
  •  先把员工的数据做一个hash映射,让员工的id和员工的信息映射起来
  •  然后就深度优先和广度优先搜索
    • DFS
    • BFS

假期是最好的休息时间也是最好的读书时间

五一快乐 好好读书 坚持运动 热爱生活 期待爱情

上一篇:什么很重要的句型


下一篇:690. 员工的重要性 (DFS 深度优先遍历)