690. 员工的重要性

https://leetcode-cn.com/problems/employee-importance/

解法一:

以id为key,Employee结构为value写入map中,为了找出所有嵌套下属员工,用递归。

"""
# Employee info
class Employee:
    def __init__(self, id, importance, subordinates):
        # It's the unique id of each node.
        # unique id of this employee
        self.id = id
        # the importance value of this employee
        self.importance = importance
        # the id of direct subordinates
        self.subordinates = subordinates
"""
class Solution:
    def getImportance(self, employees, id):
        """
        :type employees: Employee
        :type id: int
        :rtype: int
        """
        self.eMap = {}
        for e in employees:    #存入map
            self.eMap[e.id] = e
        return self.helper(id)
    
    def helper(self, id):
        imp = self.eMap[id].importance    #当前员工
        #递归访问下属员工
        for s in self.eMap[id].subordinates:
            imp += self.helper(s)
        return imp

最后更新于