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:

    1. One employee has at most one direct leader and may have several subordinates.
    2. 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;
}
};

类似题目:

Nested List Weight Sum

参考资料:

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 员工重要度的更多相关文章

  1. [LeetCode]690. Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  2. LeetCode Employee Importance

    原题链接在这里:https://leetcode.com/problems/employee-importance/description/ 题目: You are given a data stru ...

  3. Leetcode690.Employee Importance员工的重要性

    给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结构是[1 ...

  4. 690. Employee Importance员工权限重要性

    [抄题]: You are given a data structure of employee information, which includes the employee's unique i ...

  5. Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance)

    Leetcode之深度优先搜索(DFS)专题-690. 员工的重要性(Employee Importance) 深度优先搜索的解题详细介绍,点击 给定一个保存员工信息的数据结构,它包含了员工唯一的id ...

  6. 690. Employee Importance - LeetCode

    Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...

  7. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  8. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  9. 【Leetcode_easy】690. Employee Importance

    problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...

随机推荐

  1. Jmeter 后置处理器 BeanShell_PostProcessor 适用思考

    首先摘抄一段官方文档的话: The following BeanShell variables are set up for use by the script: log - (Logger) - c ...

  2. WCF跨域解决方法及一些零碎的东西。

    之前发过一篇随笔,说的WCF配置文件配置问题.里面也配了跨域支持,但是jsoncollback只支持Get请求,Post请求是解决不了,所以这里把真正的WCF跨域问题贴出来. 话不多说,直接帖配置文件 ...

  3. Java基础学习笔记二十六 JDBC

    什么是JDBC JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库.原来我们操作数据库是在控制台使用SQL语句来操作数据库,J ...

  4. 【iOS】swift 保持代码优美的10个方法

    这篇Swift风格指南与你看到的其他的指南有所不同,此篇指南主要焦点集中在打印和Web展示的可读写上.我们创建此篇风格指南的目的,是为了让我们的图书.教程以及初学者套件中的代码保持优美和一致,即使我们 ...

  5. vue初尝试--组件

    github代码同步网址 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添 ...

  6. 外网访问本地服务器下的web应用

    让本地服务器可以在外网访问的方式有很多,介绍其中一种: 由于本人是在自己电脑上装了一个虚拟机, 测试环境为:虚拟机作为服务器,服务器中装了一个禅道,虚拟机IP192.168.2.221,本人通过tpl ...

  7. JQ 标签相关知识

    1.判断 checkbox 和 radio 是否选中 if($("标签选择器").is(":checked")) 2.改变 checkbox 选中状态 .pro ...

  8. Python内置函数(60)——compile

    英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...

  9. python 字符串的方法

    capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...

  10. J2ee入门:servlet-mapping的映射配置

    <servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern> & ...