Why my Python DFS solution is so slow. Any help is appreciated.

  • 0


    def getImportance(self, employees, id):
        if not employees or not id:
            return 0
        emps = {employee.id : employee for employee in employees}
        res = emps[id].importance
        for emp in emps[id].subordinates:
            res += self.dfs(emp, emps)
        return res
    def dfs(self, id, emps):
        if len(emps[id].subordinates) == 0:
            return emps[id].importance
        ret = emps[id].importance          
        for sub_id in emps[id].subordinates:
            ret += self.dfs(sub_id, emps)
        return ret


    Big Thanks!!!!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.