Leetcode competition 179 weekly competitions (3) ------------ Notify the time required for all employees

There are n employees in the company, and each employee's ID is unique, numbered from 0 to n-1. The head of the company is identified by headID.

In the manager array, each employee has a direct person in charge, where manager [i] is the direct person in charge of the i-th employee. For the person in charge, manager [headID] = -1. The title guarantees that the affiliation can be displayed in a tree structure.

The head of the company wants to notify all employees of the company of an urgent message. He will first notify his immediate subordinates, and then these subordinates will inform their subordinates, until all employees are informed of this urgent news.

The ith employee needs informTime [i] minutes to notify all of its direct reports (that is, after informTime [i] minutes, all of his direct reports can start to spread the news).

Returns the number of minutes required to notify all employees of this emergency message.

 

Example 1:

Input: n = 1, headID = 0, manager = [-1], informTime = [0]
Output: 0
Explanation: The head of the company is the only employee of the company.
Example 2:

Input: n = 6, headID = 2, manager = [2,2, -1,2,2,2], informTime = [0,0,1,0,0,0]
Output: 1
Explanation: id = 2 The employee is the general manager of the company and the direct person in charge of all other employees. He needs 1 minute to notify all employees.


The figure above shows the tree structure of company employees.
Example 3:

Input: n = 7, headID = 6, manager = [1,2,3,4,5,6, -1], informTime = [0,6,5,4,3,2,1]
Output: 21
explained : Id = 6 for the person in charge. He will notify the employee with id = 5 within 1 minute.
The employee with id = 5 will notify the employee with id = 4 within 2 minutes.
The employee with id = 4 will notify the employee with id = 3 within 3 minutes.
The employee with id = 3 will notify the employee with id = 2 within 4 minutes.
The employee with id = 2 will notify the employee with id = 1 within 5 minutes.
The employee with id = 1 will notify the employee with id = 0 within 6 minutes.
Time required = 1 + 2 + 3 + 4 + 5 + 6 = 21.

 


Example 4:

Input: n = 15, headID = 0, manager = [-1,0,0,1,1,2,2,3,3,4,4,5,5,6,6], informTime = [1, 1,1,1,1,1,1,0,0,0,0,0,0,0,0]
Output: 3
Explanation: In the first minute, the person in charge notifies employees 1 and 2.
In the second minute they will notify employees 3, 4, 5 and 6.
In the third minute they will notify the remaining employees.
Example 5:

Input: n = 4, headID = 2, manager = [3,3, -1,2], informTime = [0,0,162,914]
Output: 1076
 

prompt:

1 <= n <= 10 ^ 5
0 <= headID <n
manager.length == n
0 <= manager [i] <n
manager [headID] == -1
informTime.length == n
0 <= informTime [i ] <= 1000
If employee i has no subordinates, informTime [i] == 0.
The title guarantees that all employees can receive notifications.

Source: LeetCode
Link: https://leetcode-cn.com/problems/time-needed-to-inform-all-employees
Copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

 

 

My code:

from collections import defaultdict
class Solution:
    def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
        tree = defaultdict(set)
        for i, e in enumerate(manager):
            if e == -1:
                root = i
            else:    
                tree[e].add(i)
        total = 0
        q = [(root, informTime[root])]
        while q:
            n_q = []
            while q:
                n, t = q.pop()
                for child in tree[n]:
                    if t + informTime[child] > total:
                        total = t + informTime[child]
                    n_q.append((child, t + informTime[child]))
            q = n_q
        return total

 

Published 230 original articles · 160 praises · 820,000 views

Guess you like

Origin blog.csdn.net/happyAnger6/article/details/104851656