[LeetCode] Employee Importance 员工重要度
You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' 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.
这道题定义了一种员工类,有id,重要度,和direct report的员工,让我们求某个员工的总重要度。我们要明白的是就算某个员工不直接向你汇报工作,而是向你手下人汇报,这个人的重要度也会算进你的重要度中,想到了狗家的统计直接汇报个数系统就是这样的。这其实就是之前那道 Nested List Weight Sum 的变化形式,我们可以用DFS来做。首先我们想,为了快速的通过id来定位到员工类,需要建立一个id和员工类的映射,然后我们根据给定的员工id来算其重要度。计算方法当然是其本身的重要度加上其所有手下人的重要度,对于手下人,还要累加其手下人的重要度。需要注意的是,像这种类似有向图遍历的问题都需要用一个HashSet来记录访问过的结点,以免有环存在,从而陷入无限死循环。但是由于这道题的场景比较特殊,一个人是不可能给自己的下属汇报的,所以不会有环存在,我们也乐得省事。建立一个结果res变量,加上当前员工的重要度,然后遍历其所有手下,对其每个手下人调用递归函数加到res上,最后返回res即可,参见代码如下:
解法一:
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> m;
for (auto e : employees) m[e->id] = e;
return helper(id, m);
}
int helper(int id, unordered_map<int, Employee*>& m) {
int res = m[id]->importance;
for (int num : m[id]->subordinates) {
res += helper(num, m);
}
return res;
}
};
我们也可以用BFS来做,使用一个queue来辅助运算,开始将给定员工id放入,然后当queue不为空进行循环,每次取出队首员工,累加上当前员工的复杂度到结果res,然后将其所有手下人加入队列等待遍历,参见代码如下:
解法二:
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int res = ;
queue<int> q{{id}};
unordered_map<int, Employee*> m;
for (auto e : employees) m[e->id] = e;
while (!q.empty()) {
auto t = q.front(); q.pop();
res += m[t]->importance;
for (int num : m[t]->subordinates) {
q.push(num);
}
}
return res;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/employee-importance/
https://leetcode.com/problems/employee-importance/discuss/112587/Java-HashMap-bfs-dfs
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Employee Importance 员工重要度的更多相关文章
- [LeetCode]690. Employee Importance员工重要信息
哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...
- LeetCode Employee Importance
原题链接在这里:https://leetcode.com/problems/employee-importance/description/ 题目: You are given a data stru ...
- Leetcode690.Employee Importance员工的重要性
给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结构是[1 ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)
Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- (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_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
随机推荐
- Matlab绘图基础——一些标准三维曲面
标准三维曲面 t=0:pi/20:2*pi; [x,y,z]= cylinder(2+sin(t),30); %[x,y,z]= cylinder(R,n),其中R为圆周半径,n为组成圆周的点 ...
- Pla
Pla(jdoj1006) 题目大意:给你n个矩形,并排放在一起,你的目的是将所有的矩形全部染色.你每次染的形状为一个矩形,问:最少需要染多少次? 注释:n<=10^6,wi , hi<= ...
- Loadrunner使用时IE浏览器打不开怎么办
1.ie浏览器去掉启用第三方浏览器扩展 2.loadrunner11 键盘F4,在browser Emulation点击change,在弹出的提示框中Browser version 选择8.0,pla ...
- shell死循环脚本示例
1.设计一个脚本,监控远程的一台机器(假设ip为192.168.0.28)的存活状态,当发现宕机时发一封邮件给你自己. 提示:1. 你可以使用ping命令 ping -c10 www.baidu. ...
- 解决exlicpe以debug模式启动或运行速度非常慢的问题
该问题可能是由于eclipse和tomcat的交互而产生的, 在以debug模式启动tomcat时,发生了读取文件错误, eclipse自动设置了断点,导致tomcat不能正常启动. 解决方法如下:以 ...
- C语言函数2
一.PTA实验作业 6-3 使用函数判断完全平方数: 1. 本题PTA提交列表: 2. 设计思路: 3.本题调试过程碰到问题及PTA提交列表情况说明: 1.一开始考虑让输入值N去整除一个循环变量i,i ...
- Beta冲刺 第三天
Beta冲刺 第三天 1. 昨天的困难 昨天的困难主要集中在对Ajax的使用上,不熟悉这种语法,所以也就浪费了时间,导致昨天的批量删除没有完全完成. 2. 今天解决的进度 潘伟靖: 1.完善了昨天没写 ...
- 每日冲刺报告——Day3(Java-Team)
第三天报告(11.4 周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...
- 数据结构与算法 —— 链表linked list(02)
我们继续来看链表的第二道题,来自于leetcode: 两数相加 给定两个非空链表来代表两个非负整数,位数按照逆序方式存储,它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了 ...
- 服务器Windows Server 2008 远程控制安全设置技巧
为了保障服务器远程控制操作的安全性,Windows Server 2008系统特意在这方面进行了强化,新推出了许多安全防范功能,不过有的功能在默认状态下并没有启用,这需要我们自行动手,对该系统进行合适 ...