Skip to content

690. Employee Importance

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

py
"""
# 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
        """
        def accum_importance(d, _id):
            if not d[_id].subordinates:
                return d[_id].importance
            importances = [accum_importance(d, s) for s in d[_id].subordinates]
            return d[_id].importance + sum(importances)

        data = {e.id: e for e in employees}
        return accum_importance(data, id)