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:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won't exceed 2000.
我的答案:
# Employee info
class Employee(object):
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(object):
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
id_list = []
importance_list = []
sub_list = []
for i in range(0, len(employees)):
id_list.append(employees[i].id)
importance_list.append(employees[i].importance)
sub_list.append(employees[i].subordinates)
id_importance_zipped = zip(id_list, importance_list)
id_sub_zipped = zip(id_list, sub_list)
id_importance_dict = dict(id_importance_zipped) # 全局字典,就是hashtable
id_sub_dict = dict(id_sub_zipped) # 全局字典
def dfs(id):
total_importance = id_importance_dict[id]
if len(id_sub_dict[id]) == 0: # 如果没下属
return total_importance
else:#如果有下属
for sub_id in id_sub_dict[id]:
total_importance = total_importance + dfs(sub_id)
return total_importance
return dfs(id)
sol=Solution()
emp1=Employee(1,5,[2,3])
emp2=Employee(2,3,[4])
emp3=Employee(3,4,[])
emp4=Employee(4,1,[])
employees=[emp1,emp2,emp3,emp4]
id=1
total=sol.getImportance(employees,id)
total
Out[1]: 13
说实话我并不是一遍写对的,在逐步探索的过程中我意识到:
1.我选了一道tag为HashTable的这道题,从网上查了一下python通过字典实现hashtable,茅塞顿开。
想分别把id-imporatnce这个键值对
和id-subordinates这个键值对
存成两个字典
2. 我意识到上述两个字典必须是全局变量,否则的话,比如本题中我举的例子:
id=1的员工有子员工id=2和id=3 ,而id=2的员工有子员工id=4, 如果上述两个字典不是全局变量,则id=4无法查找到键为4的键值对,会报Keyerror的错误
3.题意确实是深度优先搜索
综上所述写出了正确答案,但是超级慢399ms只打败了6.98%的用户。
于是看了别人更快的代码(172ms,最快):
class Solution(object):
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
return employees[id - 1].importance + sum(
[self.getImportance(employees, subordinate_id) for subordinate_id in employees[id - 1].subordinates])
比这个次之的是discussion里一个与我写法更接近,更易于理解的方法:
class Employee(object):
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(object):
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
# Time: O(n)
# Space: O(n)
emps = {employee.id: employee for employee in employees}
def dfs(id):
subordinates_importance = sum([dfs(sub_id) for sub_id in emps[id].subordinates])
return subordinates_importance + emps[id].importance
return dfs(id)
sol=Solution()
#employees=[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
emp1=Employee(1,5,[2,3])
emp2=Employee(2,3,[4])
emp3=Employee(3,4,[])
emp4=Employee(4,1,[])
employees=[emp1,emp2,emp3,emp4]
id=1
total=sol.getImportance(employees,id)
经验总结:
1. 我一开始把结果sum=sol.getImportance(employees,id)
这样的话我再调试的时候,写sum([6,7,8])
就会报TypeError: 'int' object is not callable
发现是我的变量名占用了关键字的名字(也不能占用函数的名字)
深刻教训,不能再犯
2.
sum([ ])
Out[4]: 0
sum([id for id in []])
Out[5]: 0