原题链接在这里:https://leetcode.com/problems/employee-importance/description/

题目:

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:

  1. One employee has at most one direct leader and may have several subordinates.
  2. The maximum number of employees won't exceed 2000.

题解:

类似Clone GraphNested List Weight Sum.

可以采用BFS. 每层的Employee挨个加进去.

Time Complexity: O(n). n是root id的所有下属个数, 包括直系下属和非直系下属.

Space: O(employees.size()). 全部员工生成的map.

AC Java:

 /*
// Employee info
class Employee {
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public List<Integer> subordinates;
};
*/
class Solution {
public int getImportance(List<Employee> employees, int id) {
int res = 0; HashMap<Integer, Employee> hm = new HashMap<Integer, Employee>();
for(Employee employee : employees){
hm.put(employee.id, employee);
} LinkedList<Employee> que = new LinkedList<Employee>();
que.add(hm.get(id));
while(!que.isEmpty()){
Employee cur = que.poll();
res += cur.importance;
for(int subId : cur.subordinates){
que.add(hm.get(subId));
}
} return res;
}
}

DFS.逐层往深dfs.

Time Complexity: O(n). n是root id的所有下属个数, 包括直系下属和非直系下属.

Space: O(employees.size()). 全部员工生成的map.

AC Java:

 /*
// Employee info
class Employee {
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public List<Integer> subordinates;
};
*/
class Solution {
public int getImportance(List<Employee> employees, int id) {
HashMap<Integer, Employee> hm = new HashMap<Integer, Employee>();
for(Employee employee : employees){
hm.put(employee.id, employee);
} return dfs(hm, id);
} private int dfs(HashMap<Integer, Employee> hm, int id){
int res = 0;
Employee cur = hm.get(id); res += cur.importance;
for(int subId : cur.subordinates){
res += dfs(hm, subId);
}
return res;
}
}

LeetCode Employee Importance的更多相关文章

  1. [LeetCode] Employee Importance 员工重要度

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

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

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

  3. (BFS) leetcode 690. Employee Importance

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

  4. LN : leetcode 690 Employee Importance

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

  5. 690. Employee Importance - LeetCode

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

  6. 【Leetcode_easy】690. Employee Importance

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

  7. LeetCode算法题-Employee Importance(Java实现)

    这是悦乐书的第291次更新,第309篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第159题(顺位题号是690).定义员工信息的数据结构,其中包括员工的唯一ID,他的重要 ...

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

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

  9. 【LeetCode】690. Employee Importance 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...

随机推荐

  1. OS X 与传统Unix的一点区别

    在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...

  2. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  3. json教程系列(3)-JSONObject的过滤设置

    我们通常对一个json串和java对象进行互转时,经常会有选择性的过滤掉一些属性值.例如下面的类:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  4. $《第一行代码:Android》读书笔记——第13章 Android高级技巧

    (一)全局获取Context 1.创建ApplicationUtil类继承自Application类: public class ApplicationUtil extends Application ...

  5. PHP中的常见魔术方法功能作用及用法实例

    概述 在面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利.PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发. 开始之前 在总结 ...

  6. PHP的垃圾回收机制以及大概实现

    垃圾回收,简称gc.顾名思义,就是废物重利用的意思.再说这个之前先接触一下内存泄露,大概意思就是申请了一块地儿拉了会儿屎,拉完之后不收拾,那么这块地就算糟蹋了,地越用越少,最后一地全是屎.说到底一句, ...

  7. centos下无法使用lsof命令"-bash: lsof: command not found"

    1.问题描述 : 在CentOS下,使用lsof命令,报错如下: 2.解决方法: #yum install lsof 若输入y不能安装成功,通过yum install 包 -y 进行安装: # yum ...

  8. java入门了解04

    1.异常的体系:---------|Throwable --------------| Error (错误) 错误一般是由于jvm或者是硬件引发的问题,所以我们一般都不会通过代码去处理.------- ...

  9. vs 2010 mvc 3.0安装软件

    下载链接如下:MVC 3安装包:http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=d2928bc1-f48c-4e95-a0 ...

  10. YARN作业提交流程剖析

    YARN(MapReduce2) Yet Another Resource Negotiator / YARN Application Resource Negotiator对于节点数超出4000的大 ...