1 class Solution:
 2     def numOfMinutes(self, n: int, headID: int, manager: 'List[int]', informTime: 'List[int]') -> int:
 3         graph = collections.defaultdict(list)
 4         
 5         for i in range(n):
 6             if manager[i] !=-1:
 7                 graph[manager[i]].append(i)
 8         
 9         if not graph:
10             return 0
11 
12         res  = 0
13         stack = [(headID,informTime[headID])]
14         while stack:
15             node,cost = stack.pop(0)
16             res = max(res,cost)
17             for x in graph[node]:
18                 stack.append((x,cost + informTime[x]))
19         
20         return res

算法思路:BFS。

参考:https://leetcode.com/problems/time-needed-to-inform-all-employees/discuss/545672/Python-Simple-DFS

posted on 2020-03-25 08:01  Sempron2800+  阅读(181)  评论(0编辑  收藏  举报