题目要求

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. VS2013 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    全是2012版本的  没找到2013的! 控制面板>程序>程序和功能 找到如下选中软件右击修复 即可 需关闭VS2013 参考:https://blog.csdn.net/zhaoyun9 ...

  2. linux基础知识 【转】

    linux目录架构 / 根目录 /bin 常用的命令 binary file 的目錄 /boot 存放系统启动时必须读取的档案,包括核心 (kernel) 在内 /boot/grub/menu.lst ...

  3. Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用

    下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...

  4. Maven手动安装jar包到仓库

    mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\IKAnalyzer6.5.0.jar -DgroupId=com.luc ...

  5. Linux下MySQL5.7.18二进制包安装(无默认配置文件my_default.cnf)

    最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5.7.18)二进制包,CentOS7.2下测试安装,方便以后折腾.大概步骤如下,安装删除反复折腾了几 ...

  6. Scala学习笔记(二):object、伴生对象和基本类

    object object 是只有一个实例的类.它的定义与Java中的class类似,如: // 单例对象 object AppEntry { def main(args: Array[String] ...

  7. 【QT】打开文件对话框,选择路径下文件

    0.头文件中加入 public: QString fileName; public slots: void showImage(); 1.添加两个头文件 #include<qfiledialog ...

  8. [React] 08 - Tutorial: evolution of code-behind

    有了七篇基础学习,了解相关的知识体系,之后便是系统地再来一次. [React] 01 - Intro: javaScript library for building user interfaces ...

  9. 排查java进程cpu占用高的问题

    一.思路 分两步,主要是找出占用cpu高的进程,再找出该进程内到底是哪个线程占用cpu高. 二.找出占用cpu高的进程 参考: https://blog.csdn.net/hfhwfw/article ...

  10. 【CF582E】Boolean Function 树形DP+FWT

    [CF582E]Boolean Function 题意:给你一个长度为n的表达式,其中未知数有A,B,C,D和?,运算有&和|和?(表达式中用括号确定了唯一的运算顺序).?代表A,B,C,D或 ...