题目要求

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.

题目分析及思路

定义了一个含有employee信息的数据结构,该结构包括了employee的唯一id、他的重要值以及他的直接下属的id。现给定一个公司的employee的信息,以及一个employee的id,要求返回这个employee以及他所有直接下属的重要值的总和。可以使用递归的方法,跳出递归的条件是当前employee没有直接下属,否则则遍历当前employee的所有下属获得重要值。

python代码

"""

# 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

:type id: int

:rtype: int

"""

ids = [employee.id for employee in employees]

leader = employees[ids.index(id)]

employees.pop(ids.index(id))

if leader.subordinates == []:

return leader.importance

else:

im = 0

for id in leader.subordinates:

im += self.getImportance(employees, id)

return im + leader.importance

LeetCode 690 Employee Importance 解题报告的更多相关文章

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

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

  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. LeetCode 690. Employee Importance (职员的重要值)

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

  5. LeetCode - 690. Employee Importance

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

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

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

  7. [LeetCode]690. Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  8. 690. Employee Importance - LeetCode

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

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. java实现栈-使用LinkedList

    import java.util.LinkedList; public class LinkedListStack { public static void main(String[] args) { ...

  2. Kiss MySQL goodbye for development and say hello to HSQLDB

    The days of using MySQL, DB2, PostgreSQL etc for development is  over.. I don’t know why any program ...

  3. 20 go单元测试

    单元测试 Go本身提供了一套轻量级的测试框架.符合规则的测试代码会在运行测试时被自动识别并执行.单元测试源文件的命名规则如下: 必须是以_test.go结尾的文件,比如manager_test.go ...

  4. android bionic c 对比 gnu c

    Bionic 是一个BSD标准的C库,用在android平台上面的. Android 是一个不完全开源的系统. android的kernel使用的是基于linux的,linux使用的是GPL2的开源标 ...

  5. Nginx-配置一个简单的http虚拟服务

    配置文件内容如下: #user nobody; worker_processes 4; #工作进程的个数,可以配置多个,一般配置成CPU的核数 pid logs/nginx.pid; # 此文件用于记 ...

  6. C语言程序设计--执行命令

    1.system函数 1.1函数原型 int system(char *command); 1.2解释 system()会调用fork()产生子进程,由子进程来调用/bin/sh -c string来 ...

  7. GDI+绘制五星红旗

    五星红旗是由红色背景,加5个黄色五角星组成.绘制一个五星红旗的思路,就是先定义一个五角星的自定义控件,然后通过设置五角星的大小.位置.旋转角度等属性,组合成一个五星红旗. 五角星自定义控件代码: pu ...

  8. 使用SpringContextHolder获取bean实例

    public static IConstantFactory me(){ return SpringContextHolder.getBean(beanName:"constantFacto ...

  9. 英文写作强调技巧:alliteration, assonance, consonance

    alliteration 安排两个以上首字母相同的词相邻 例: The bouncing ball went high into the sky. (In this example, the &quo ...

  10. FIFO设计中的深度计算【zz】

    FIFO设计中的深度计算: 写时钟频率 w_clk, 读时钟频率 r_clk, 写时钟周期里,每B个时钟周期会有A个数据写入FIFO: 读时钟周期里,每Y个时钟周期会有X个数据读出FIFO: 则,FI ...