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. Geeks面试题:Min Cost Path

    Min Cost Path   Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function tha ...

  2. Android程序对不同手机屏幕分辨率自适应的方法

    相信各位Android开发爱好者都知道,由于OEM之间的竞争,各种Android操作系统的手机简直就是琳琅满目,屏幕分辨率的差异可想而知.目前比较主流的有WVGA=800x480,HVGA=480x3 ...

  3. C#访问MySQL数据库帮助类

    MySQL数据库访问帮助类 1.项目添加引用官方MySQL动态库MySql.Data.dll 下载地址:MySql.Data.dll(也可以到官网下载动态库)项目添加引用 这里有一个Mysql帮助类的 ...

  4. 部署OpenStack问题汇总(一)--使用packstack安装openstack:源问题的处理

    在安装的过程中,遇到了源的问题,找不到包的网页:    重新打开 预装源地址,打开epel-openstack-havana.repo 文件,显示如下: # Place this file in yo ...

  5. docker 参数

    -a, --attach=[] Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项 --add-host= ...

  6. python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

    一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...

  7. border-image使用过程中遇到的几个问题

    这次年货,为了增添气氛,很多地方都用了边框,由于边框高度的不固定,给构建着实带了不小的麻烦. 通常我们处理边框的方法,无非以下两种方案: 1.切整张图片,做背景. 2.切上中下,进行拼接处理. 但是遇 ...

  8. node path的几个路径问题

    __dirname: 总是返回被执行的 js 所在文件夹的绝对路径 __filename: 总是返回被执行的 js 的绝对路径 process.cwd(): 总是返回运行 node 命令时所在的文件夹 ...

  9. dataframe转换为多维矩阵,然后可以使用values来实现

    import pandas as pd import numpy as np df = pd.DataFrame(np.random.rand(3,3),columns=list('abc'),ind ...

  10. mysql多列索引优化

    “把Where条件里面的列都建上索引”,这种说法其实是非常错误的! 这样一个查询,假设actor_id与film_id都单独建立索引 SELECT film_id , actor_id FROM sa ...