Leetcode 690

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011240016/article/details/82983260

问题描述:简单型,BFS解法

You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his directsubordinates’ id.

For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.

Example 1:

Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
Explanation:
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.

Note:

  1. One employee has at most one direct leader and may have several subordinates.
  2. The maximum number of employees won’t exceed 2000.

40/108

"""
# 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 # 是个列表,列表中的数据类型是Employee
        :type id: int
        :rtype: int
        """
        weight_sum = 0
        ids = [id] # 用于存储相关的id
        
        while employees != []:
            employee = employees.pop(0) # 排在第一的不一定是最高领导,乱序
            
            [cur_id, weight, sub_lst] = employee.id,employee.importance, employee.subordinates 
            
            if cur_id in ids:
                weight_sum += weight
                ids += sub_lst
        return weight_sum
    
        

93/108

"""
# 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 # 是个列表,列表中的数据类型是Employee
        :type id: int
        :rtype: int
        """
        weight_sum = 0
        ids = [id] # 用于存储相关的id
        temp = employees[:] # 深度复制
        # 第一遍遍历加入所有的id
        
        while temp != []:
            temp_employee = temp.pop(0) # 排在第一的不一定是最高领导,乱序
            [cur_id, weight, sub_lst] = temp_employee.id,temp_employee.importance, temp_employee.subordinates 
            if cur_id in ids:
                ids += sub_lst
        
        # 第二遍遍历计算weight_sum
        while employees != []:
            employee = employees.pop(0)
            [cur_id, weight, sub_lst] = employee.id, employee.importance, employee.subordinates 
            if cur_id in ids:
                weight_sum += weight
        return weight_sum

这种都是思想含有漏洞的解法,正确、全面的思路应当是结合map数据结构进行。

想到用字典/map结构来进行数据处理,问题将变得非常简单:

"""
# 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 # 是个列表,列表中的数据类型是Employee
        :type id: int
        :rtype: int
        """
        dic = {} # 用于列表变字典
        total = 0
        for e in employees:
            dic[e.id] = [e.importance, e.subordinates]
        ids = [id] # 用于模拟queue
        while ids != [] :
            cur_id = ids.pop(0)
            total += dic[cur_id][0]
            ids += dic[cur_id][1]
        return total

一旦列表编程以id作为键,重要性和下属作为值以后,就可以顺藤摸瓜,再用队列来走一遍,此时字典已经就位,就可以按照直接下属关系来游走了。

开始想建立一棵树,但是很麻烦,也没有想清楚细节,结合字典来处理,建树也变得简单起来。

END.

猜你喜欢

转载自blog.csdn.net/u011240016/article/details/82983260