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. CentOS安装JDK-tar.gz文件

    1.下载JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. 测试 ...

  2. Win10系列:UWP界面布局进阶5

    提示框 在Windows应用商店应用程序中可以使用提示框来向用户显示提示信息,例如可以通过对话框来询问用户当前需要执行的操作,还可以通过弹出窗口来显示需要注意的信息.本节将向读者介绍如何在Window ...

  3. Win10系列:VC++媒体播放控制3

    (5)添加视频进度条 视频进度条可以用来显示当前视频的播放进度,并可以通过拖动视频进度条来改变视频的播放进度.接下来介绍如何实现视频进度条,首先打开MainPage.xaml文件,并在Grid元素中添 ...

  4. Debug method

    #define DEBUG(format,...) printf("Ray.he file:"__FILE__" func:%s() line:%d, print &qu ...

  5. day_07_python_1124

    01 昨日内容回顾 数据类型补充: str <---> list split join list <---> set set(list) list(set()) list &l ...

  6. shlve 模块

    shlve 模块  也用于序列化 它与pickle 不同之处在于 不需要惯性文件模式什么的 直接把它当成一个字典来看待 它可以直接对数据进行修改 而不用覆盖原来的数据 而pickle 你想要修改只能 ...

  7. WPF 创建自定义控件及自定义事件

    1 创建自定义控件及自定义事件 /// <summary> /// 演示用的自定义控件 /// </summary> public class ExtButton : Butt ...

  8. Mysql 行存储的文件格式

    一.Mysql行存储的文件格式概述 InnoDB存储引擎有两种文件格式 Antelope:compact与redundant两种行记录格式 Barracuda:compress与dynamic两种行记 ...

  9. Python Oracle连接与操作封装

    一.封装方式一 #encoding:utf-8 import cx_Oracleclass Oracle_Status_Output:    def __init__(self,db_name,db_ ...

  10. uitableView group模式下的间距问题

    我么在使用group模式定义tableview的时候,系统默认是会有head和foot的间距的,来区分我们不同的group:在具体使用的时候又时候我们不需要这个间距.我们可以重新赋值这些间距来达到我们 ...