[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 ...
随机推荐
- 怎样更改SQL Server 2008的身份验证方式
大家都知道sql server 有两种登录验证方式,即sql server验证方式和windows验证方式,但是sql server默认的是windows登录验证方式,我们如何启用sql server ...
- Shell 中的反引号(`),单引号('),双引号(")
在写shell的时候老是傻傻分不清楚,今天来理一理. 1.反引号位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引号(')位于Enter键的左方的区别. 在Linux中起着命令替换的作用.命 ...
- Qt编写的RTSP播放器+视频监控(vlc版本)
几天写了个ffmpeg版本,今天特意抽空改写个vlc版本,之前vlc播放视频后,被接管了不能识别到鼠标,需要重新编译vlc源码得到支持鼠标消息的版本. /*** vlc视频播放类 作者:feiyang ...
- H.264 White Paper学习笔记(一)总览
H.264 White Paper对于264编码器的原理讲的比较透彻,在阅读学习的时候收获很大,这份文献网上有很多了,也有不少人翻译,不过想要理解更清楚我觉得还是得看英文原版的. 首先看一下白皮书里给 ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- VMware ESXI5.5 Memories limits resolved soluation.
在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...
- Memcached存Session数据、访问安全性、使用场景总结(3)
最近做了一个单点登录SSO,登陆后的凭证放到Memcached令牌放到Cookies:但是用户经常掉线,开发环境和测试却没有这个问题,最后从Memcached找到原因. Memcached概念.作用. ...
- Win8安装msi程序出现2502、2503错误解决方法
在Win8中,在安装msi安装包的时候常常会出现代码为2502.2503的错误.其实这种错误是由于安装权限不足造成的,因为这种msi的安装包不像其他exe的安装程序, 在安装包上点击"右键& ...
- C++ 标准输出cout与printf
C++标准输出cout与printf都可以,printf用法更死板一些. #include <iostream> int main(int argc, char** argv) { usi ...
- centos6.9环境下JDK安装部署
1.准备jdk安装文件: 这里我使用的是 jdk-7u79-linux-x64.tar.gz 2.在 /usr/local 目录下创建 sotfware目录,并上传JDK文件: 解压文件并修改文件夹为 ...