690. Employee Importance
Easy

377369FavoriteShare

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.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这个题是可以用BFS,也可以用DFS,不过,关于C++的类与指针的用法一度难倒了我。。。。

C++代码:

/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
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;
}
};

(BFS) leetcode 690. Employee Importance的更多相关文章

  1. LN : leetcode 690 Employee Importance

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

  2. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  3. LeetCode 690. Employee Importance (职员的重要值)

    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 ...

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

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

  7. 690. Employee Importance - LeetCode

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

  8. 【Leetcode_easy】690. Employee Importance

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

  9. [LeetCode] 690. Employee Importance_Easy tag: BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

随机推荐

  1. Play framework框架中通过post方式发送请求

    搞了好久这个最终还是在play官方文档中看见的发送请求的方式,国内好像很少有使用这个框架的,加之自己不是太愿意宣传,好东西总归是好东西,不说废话了. 在play中发送请求有两种常用的方式,一种get, ...

  2. 解决Error:com.intellij.util.indexing.StorageException

    删除 C:\Users\Nihaorz\.IntelliJIdea2017.1\system\compile-server 目录下的所有内容即可

  3. BZOJ3223文艺平衡树——非旋转treap

    此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  4. 基准对象object中的基础类型----元组 (五)

    object有如下子类: CLASSES object basestring str unicode buffer bytearray classmethod complex dict enumera ...

  5. hbase系列

    jvmhttps://www.cnblogs.com/jiyukai/p/6665199.html hbase https://blog.csdn.net/lizhitao/article/detai ...

  6. 贝叶斯定理推导(Bayes' Theorem Induction)

    这里用Venn diagram来不严谨地推导一下贝叶斯定理. 假设A和B为两个不相互独立的事件. 交集(intersection):  上图红色部分即为事件A和事件B的交集. 并集(union):  ...

  7. [AHOI2008]紧急集合 / 聚会

    题目描述 欢乐岛上有个非常好玩的游戏,叫做“紧急集合”.在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等待点,通过道路从一个点到另一个点要 ...

  8. BZOJ 5097: [Lydsy1711月赛]实时导航(最短路 + bitset)

    题意 \(n​\) 个点的有向图,边权 \(\in \{1, 2, 3, 4\}​\) ,\(m​\) 次修改边权/加边/删边,\(q​\) 次询问:以 \(s_i​\) 为起点,输出它到其他点的最短 ...

  9. opencontrail-vrouter命令

    vif命令 vrouter需要vrouter接口(vif)来转发流量.使用vif命令查看vrouter已知的接口. 注意: 仅在OS(Linux)中使用接口不足以进行转发.相关接口必须添加到vrout ...

  10. photoshop学习4

    蒙版 路径学习 一.蒙版 蒙版可以理解为一层在图层上的遮挡布,为什么要将图层遮住呢,有什么好处.好处在于容易编辑. 在一个图层上建立一个蒙版之后,可以用再删掉不需要的部分,从而露出原图层的部分.那么这 ...