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:

  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或者BFS, 这里我用BFS, 只是需要注意的一点是input 是employee 为data structure, 但是只需要用两个dictionary来分别存相对应的数值就好, 换汤不换药. 本质还是BFS.

1. constrains:

1) input 的max size 为2000, 可以为[]

2) edge: maybe id not in employees. 此时, 直接return ans(init:0)

2. ideas:

BFS:      T: O(n)  # n 为number of employees

S: O(n) # 2n dictionary, n queue, so total is O(n)

1) d1, d2, ans 分别存importance, subordinates, 0

2) if id not in d1, return ans   # edge

3) queue (init: [id]), visited(init: set())

4) while queue: new_id = queue.popleft(), ans += new_id.importance, visited.add(new_id)

5) for each in new_id.subordinates: 判断是否visited过, 如果没有, queue.append(each)

6) 结束while loop, return ans

3. code

 class Solution:
def getImportance(self, employees, id):
d1, d2, ans = {}, {}, 0
for e in employees:
d1[e.id] = e.importance
d2[e.id] = e.subordinates
if id not in d1: return ans
queue, visited = collections.deque([id]), set()
while queue:
new_id = queue.popleft()
ans += d1[new_id]
visited.add(new_id)
for each in d2[new_id]:
if each not in visited and each in d1: # add each in d1 incase input [[1,5, [2]]] like this, but test cases dont include, 多思考点不是坏处
queue.append(each)
return ans

4. test cases

1) [], 1

2) [[1, 4, []]], 2

3) [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1

[LeetCode] 690. Employee Importance_Easy tag: BFS的更多相关文章

  1. (BFS) leetcode 690. Employee Importance

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

  2. LN : leetcode 690 Employee Importance

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

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

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

  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 解题报告

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

  7. [LeetCode] 490. The Maze_Medium tag: BFS/DFS

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  8. [LeetCode] 577. Employee Bonus_Easy tag: SQL

    Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...

  9. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

随机推荐

  1. 游戏服务器学习笔记 4———— master 模块介绍

    (模块的介绍方法都是先说大体功能,在捡一些细节详细讨论.) master 类很简单,就3个函数,一个init,设置配置信息,并调用masterapp,然后还有一个循环启动子进程的start函数. 这里 ...

  2. python爬虫---->scrapy的使用(一)

    这里我们介绍一下python的分布式爬虫框架scrapy的安装以及使用.平庸这东西犹如白衬衣上的污痕,一旦染上便永远洗不掉,无可挽回. scrapy的安装使用 我的电脑环境是win10,64位的.py ...

  3. shell 数值计算

    shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法,以后用到的时候可以来看,呵呵.   1.错误方法举例   a)   var=1+1   echo $var  ...

  4. vue笔记 - 组件间通信 之 中央事件总线bus

    中央事件总线 - 就是一个名字可以叫做bus的vue空实例,里边没有任何内容: var bus = new Vue(); 人如其名,她就像一个公交车一样,来回输送人,将a站点的A输送到b站点,再将b站 ...

  5. java(9)并发编程

    整理自<java 并发编程的艺术> 1. 上下文切换 即使是单核处理器也支持多线程执行代码,CPU通过给每个线程分配CPU时间片来实现这个机制.时间片是CPU分配给各个线程的时间,因为时间 ...

  6. 题目1162:I Wanna Go Home(最短路径问题进阶dijkstra算法))

    题目链接:http://ac.jobdu.com/problem.php?pid=1162 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  7. 用ChrootDirectory限制SFTP登录的用户只能访问指定目录且不能进行ssh登录

    创建不能ssh登录的用户sftpuser1,密码用于sftp登录: sudo adduser sftpuser1 --home /sftp/sftpuser1 --shell /bin/false s ...

  8. DragonBones龙骨插槽的隐藏

    参考: 插槽中对象的显示与隐藏问题 let factory = dragonBones.EgretFactory.factory; factory.parseDragonBonesData(RES.g ...

  9. 使用COSBench工具对ceph s3接口进行压力测试--续

    之前写的使用COSBench工具对ceph s3接口进行压力测试是入门,在实际使用是,配置内容各不一样,下面列出 压力脚本是xml格式的,套用UserGuide文档说明,如下 有很多模板的例子,在co ...

  10. 配置java环境变量后没有生效的解决办法

    参考文章:https://blog.csdn.net/tooky_poom/article/details/60768458 系统安装了jdk1.7,环境变量正常,但是安装jdk1.8后,修改环境变量 ...