5354. notification time required for all employees

N the company has employees, each employee's ID is unique, numbered from 0 to n - 1. The company's overall responsibility is identified by headID.

In the array manager, each employee has a person in charge of the immediate, where manager [i] is directly under the head of the i-employees. The total charge, manager [headID] = -1. Title ensure affiliation may be displayed in a tree structure.

The company's total official wants an urgent message to all employees notice. He will first inform his immediate subordinates, then inform their subordinates from these subordinates until all employees are informed of this urgent message.

I-employees need informTime [i] minutes to notify all of its immediate subordinates (that is to say in informTime [i] minutes later, all his immediate subordinates can begin to spread the news).

Returns the number of minutes to inform all employees that needed urgent message.

Example 1:

Input: n = 1, headID = 0 , manager = [-1], informTime = [0]
outputs: 0
to explain: overall responsibility for the company is the only one of the company's employees.

1. When the game did not do it, not thinking clearly. . In fact, a very simple idea is traversed from the bottom up, recording the maximum. . That is, from the bottom-up dfs

2. can also be top-down, but I have to get my leadership Shoudexia what are human relationships, in dfs

class Solution(object):
    def numOfMinutes(self, n, headID, manager, informTime):
        """
        :type n: int
        :type headID: int
        :type manager: List[int]
        :type informTime: List[int]
        :rtype: int
        """
        res = 0
        for i in range(len(manager)):
            if informTime[i]==0:
                tmp_res = 0
                index = i
                while index!=-1:
                    tmp_res += informTime[index]
                    index = manager[index]
                res = max(res,tmp_res)
        return res

from collections import defaultdict
class Solution(object):
    def numOfMinutes(self, n, headID, manager, informTime):
        """
        :type n: int
        :type headID: int
        :type manager: List[int]
        :type informTime: List[int]
        :rtype: int
        """
        self.total = 0
        def dfs(tmp, informTime, head_id, total):
            if not tmp[head_id]:
                self.total = max(self.total,total)
            for id in tmp[head_id]:
                dfs(tmp,informTime,id,total+informTime[head_id])
        tmp = defaultdict(list)
        for i in range(len(manager)):
            tmp[manager[i]].append(i)
        dfs(tmp,informTime,headID,0)
        return self.total

 

 

Published 200 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_36328915/article/details/104748822