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. springbatch---->springbatch的使用(七)

    这里我们讲述一下springbatch中关于step层面上面的数据共享技术.而对街的人影都浸染在一片薄荷的白色中,由于无声,都好像经过漂染,不沾人间烟火. step的数据共享 关于springbatc ...

  2. Shell xargs

    from here 简介之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: 这个命令是错误的find /sbin -perm + ...

  3. MFC 三种消息

    在MFC应用程序中传输的消息有三种类型:窗口消息.命令消息和控件通知. (1)窗口消息:WM_XXX 窗口消息(Window Message)一般与窗口的内部运作有关,如:创建窗口.绘制窗口和销毁窗口 ...

  4. Oracle 12C 创建用户连接pdb

    测试环境: C:\ora12c\product\12.1.0\dbhome_1\BIN>sqlplus.exe /nolog SQL*Plus: Release 12.1.0.1.0 Produ ...

  5. Elasticsearch学习之深入聚合分析五---案例实战

    1. fielddata核心原理 fielddata加载到内存的过程是lazy加载的,对一个analzyed field执行聚合时,才会加载,而且是field-level加载的,一个index的一个f ...

  6. 路由器子网掩码设置不正确导致github无法访问

    奇怪的现象,路由器子网掩码设置成255.0.0.0会导致电脑访问不到https://github.com/ 改成默认的255.255.255.0就正常了.

  7. react封装组织架构递归树

    想用react实现一个递归树,但一些框架里面的有些不符合需求,于是自己写了个,功能比较简单,欢迎批评指正.. react实现这样一个组织架构递归树,下级部门的收起和展开,点击部门名称时请求接口获取下级 ...

  8. Druid在有赞的实践

    转载一篇自己在公司博客上的文章 一.Druid介绍 Druid 是 MetaMarket 公司研发,专为海量数据集上的做高性能 OLAP (OnLine Analysis Processing)而设计 ...

  9. hihocoder 1331 - 扩展二进制数 - [hiho一下168周]

    题目链接:http://hihocoder.com/problemset/problem/1331 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们都知道二进制数的每 ...

  10. 安装XP时BIOS的设置(ahci ide)

    和以前使用Windows XP一样,很多用户都在设法提高Windows 7的系统运行速速,比较常见的方法大多是对系统服务进行优化,去掉一些可有可无的系统服务,还有就是优化资源管理器菜单等.除此之外,还 ...