You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' 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.
 
DFS
 
"""
# 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
"""
emap={e.id:e for e in employees}
def dfs(sid):
em=emap[sid]
return em.importance+sum(dfs(sid) for sid in em.subordinates)
return dfs(id)

  

[LeetCode&Python] Problem 690. Employee Importance的更多相关文章

  1. 【Leetcode_easy】690. Employee Importance

    problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...

  2. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  3. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  4. 690. Employee Importance - LeetCode

    Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...

  5. 【LeetCode】690. Employee Importance 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...

  6. LeetCode 690 Employee Importance 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  7. LeetCode 690. Employee Importance (职员的重要值)

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  8. LeetCode - 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  9. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

随机推荐

  1. ForkJoin

    Java Fork/Join 框架 jdk1.8-ForkJoin框架剖析 Java的Fork/Join任务,你写对了吗? 概述 从JDK1.7开始,Java提供Fork/Join框架用于并行执行任务 ...

  2. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

  3. ActiveMQ producer 流量控制

    http://activemq.apache.org/producer-flow-control.html 翻译: 流量控制是指:如果broker检测到destination的内存限制.temp文件限 ...

  4. windows 下的命令操作

    删除文件夹 RD /S D:\aaaaa 删除文件夹下的文件 DEL D:\aaaaa\*.*

  5. js 数组api

    Javascript Array API   JS数组对象提供了很多API方法,要用到的朋友可以查阅哈,如有错误欢迎指正. /** * Created by Administrator on 2017 ...

  6. rational rose java.lang.classNotFoundException

            C:\Windows\Java\TrustLib\RoseDataModeler.zip;C:\Windows\Java\TrustLib\comwrappers.zip;C:\Win ...

  7. ubuntu启用root登陆

    ubuntu系统不能够默认以root用户登陆系统如果你为了方便开发想每次登陆的时候以root用户登陆那么需要手动的做写更改打开终端 首先输入命令 sudo passwd  root更新你的密码然后输入 ...

  8. laravel 查询指定字段的值

    $this->model->where('id',$id)->value('user');

  9. xadmin后台分段导出避免timeout

    一.问题 xadmin后台功能很强大,特别在导出的时候格式有xls/xlsx.csv.xml.json.实际常用的还是前面2种.xls格式使用的xlwt,有个缺陷,导出数据过大时,会报ValueErr ...

  10. WPF 基于Adorner实现类似Popup效果

    1.  什么是Adorner 装饰器是一种特殊类型的FrameworkElement,可用来向用户提供可视提示. 装饰器有很多用途,可用来向元素添加功能句柄,或者提供有关某个控件的状态信息. 2.  ...