[LeetCode] 690. Employee Importance_Easy tag: BFS
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:
- One employee has at most one direct leader and may have several subordinates.
- 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的更多相关文章
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- [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 ...
- [LeetCode] 577. Employee Bonus_Easy tag: SQL
Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+---- ...
- [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 ...
随机推荐
- DELPHI XE Android 开发笔记
第一次编译时,设定android SDK: F:\RAD Studio XE6\PlatformSDKs\adt-bundle-windows-x86-20131030\sdk F:\RAD Stud ...
- python框架---->pymysql的使用
这里面学习一下python中操作mysql的第三方库pymysql的使用.很多我们以为一辈子都不会忘掉的事情,就在我们念念不忘的日子里.被我们遗忘了. pymysql的简单使用 我们创建一张表来进行下 ...
- 【Spring源码深度解析学习系列】Bean的加载(六)
Bean的加载所涉及到的大致步骤: 1)转换对应beanName 为什么需要转换beanName呢?因为传入的参数可能是别名,也可能是FactoryBean,所以需要一系列的解析,这些解析内容包括如下 ...
- Android JNI与多线程
Java通过JNI调用本地C++代码是在同一个线程中的同步调用. JNI中如果新建的线程调用java的代码,那么java代码是运行在JNI线程中的:但是,如果调用UI相关的代码时需要与java主线程通 ...
- C# 拆箱与装箱及优化
1.概念 装箱在值类型向引用类型转换时发生,在堆中分配. 拆箱在引用类型向值类型转换时发生. 2.装箱拆箱的过程 //这行语句将整型常量1赋给object类型的变量obj:众所周知常量1是值类型,值类 ...
- Office word 2007不能另存为pdf格式的解决方法
我们在使用Office word 2007时,经常会使用到另存为 PDF 或 XPS(P),遗憾的是,很多人都找不到这个选项, 或者在安装word的时候,并没有安装该加载项,需要你在后期安装,我们来怎 ...
- 【CF878E】Numbers on the blackboard 并查集
[CF878E]Numbers on the blackboard 题意:给你一个长度为n个数列,你每次可以进行如下操作: 选取两个相邻的数x,y(x在y左面),然后将这两个数去掉,用x+2y替换它. ...
- jfinal如何获取参数为数组的值
前台: var postParams = { ids:["qw","ew"]}; 后台: String[] userids = this.getParaValu ...
- Python中常用包——sklearn主要模块和基本使用方法
在从事数据科学的人中,最常用的工具就是R和Python了,每个工具都有其利弊,但是Python在各方面都相对胜出一些,这是因为scikit-learn库实现了很多机器学习算法. 加载数据(Data L ...
- MongoDB 学习笔记2----条件操作符
条件操作符:用于两个比较两个表达式并从mongdb中获取文档 mongodb常见的操作符及解析说明 $lt:小于 example:ago<20 $lte:小于等于 example:<=20 ...