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. android第三方---->android智能机器人的使用

    在网上找了个第三方智能机器人,可以实现聊天语音等功能,比较不错的.今天我们就开始智能机器人聊天的学习,例子中涉及的handler的有关知识和json数据的解析,请参见我的博客:android基础--- ...

  2. Free Download Manager (FDM) 中文版 - 替代迅雷最佳免费开源下载工具软件

    https://www.freedownloadmanager.org/ Free Download Manager (FDM) 是一款经典免费纯粹的下载软件,它开源无广告,界面简洁清爽,支持 BT. ...

  3. sencha touch datepicker/datepickerfield(时间选择控件)扩展(废弃 仅参考)

    参考资料:https://market.sencha.com/extensions/datetimepicker 上面的扩展在2.2有些问题,参考源码重新写了一个 TimePicker: Ext.de ...

  4. ubuntu1304无法启动桌面系统的问题和解决

    今天上班,从oracle官网下载个最新的virtual box,安装后重启电脑,进入桌面后竟然没有菜单栏和启动栏了(就是最上边的bar和左边的应用栏),而且所有启动的窗口都没有菜单栏,终端什么的也都没 ...

  5. OOA/D 01

    建筑师一般不会为一栋100层的楼添加一个新的地下室,因为成本太高无疑会失败,但软件系统里提出类似改动需求时,他们通常都不会多想一下,相反他们会说:这只是一个简单的编程问题 可总会有一些看似极难完成.但 ...

  6. WF的简单使用

    WWF(Windows Workflow Foundation):是微软提供的工作流技术,工作流就是对工作流程的规范和抽象.主要有三个部分Activity(活动).Runtime(工作流运行时)和To ...

  7. HOJ 1444 Humble Numbers

    Humble Numbers My Tags (Edit) Source : University of Ulm Internal Contest 1996 Time limit : 1 sec Me ...

  8. TOP100summit 2017:投资千亿成立达摩院,揭秘阿里在人工智能领域的探索

    今天上午,阿里巴巴云栖大会在杭州开幕,第一条重磅消息是阿里首席技术官张建锋宣布成立达摩院,在全球各地建立实验室,3年内投入千亿在全球建立实验室.和高校建立研究所.建立全球研究中心等事务. 该院由全球实 ...

  9. OpenCV学习笔记之课后习题练习2-3

    3.使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘. 例2-10中所用的方法虽然能正常运行,但却不能 ...

  10. HDFS文件上传

    下图描述了Client向HDFS上传一个200M大小的日志文件的大致过程: 1)首先,Client发起文件上传请求,即通过RPC与NameNode建立通讯. 2)NameNode与各DataNode使 ...