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.

题目标签:HashTable

  题目给了我们一个 employees list, 和 一个 id,让我们找到这个 id 的员工的手下所有员工的 importance 累加,包括他自己的。

  首先把 employees 存入 HashMap, id 为 key, Employee 为value。

  然后建立一个 dfs function:

    当员工的 subordinates 的 size  等于 0 的时候, 说明没有必要继续递归了,返回员工的重要值;

    如果 size 大于0,那么遍历 subordinates,把每一个 员工id 递归,累加重要值。

Java Solution:

Runtime beats 71.9%

完成日期:11/16/2017

关键词:HashMap, DFS

关键点:把employee 信息存入map,id 为 key,employee 为 value,便于dfs 直接调取 员工信息

 /*
// 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> map = new HashMap<>(); for(Employee e: employees)
map.put(e.id, e); return dfs(id, map);
} private int dfs(int id, HashMap<Integer, Employee> map)
{
Employee e = map.get(id); if(e.subordinates.size() == 0)
return e.importance; int imp = e.importance; for(int sub: e.subordinates)
imp += dfs(sub, map); return imp;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 690. Employee Importance (职员的重要值)的更多相关文章

  1. (BFS) leetcode 690. Employee Importance

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

  2. LN : leetcode 690 Employee Importance

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

  3. LeetCode 690 Employee Importance 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  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——本质上就是tree的DFS和BFS

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

  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 解题报告(Python)

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

随机推荐

  1. dedecms手机网站内页上一篇/下一篇的翻页功能

    修改文件include/arc.archives.class.php文件. 1.搜索 function GetPreNext($gtype='') 2.将这个函数的所有内容替换为 function G ...

  2. ubuntu部署java环境

    一.安装java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracl ...

  3. UI布局只关乎三件事情:尺寸、位置、组织

    UI布局只关乎三件事情:尺寸.位置.组织. 组织分为两类: 单元组织: 集合组织: 混合组织.

  4. RocketMQ学习笔记(16)----RocketMQ搭建双主双从(异步复制)集群

    1. 修改RocketMQ默认启动端口 由于只有两台机器,部署双主双从需要四个节点,所以只能修改rocketmq的默认启动端口,从官网下载rocketmq的source文件,解压后使用idea打开,全 ...

  5. 梦想CAD控件 2019.01.20更新

    下载地址:http://www.mxdraw.com/ndetail_10120.html1. 修改CAD不等比例块保存问题2. 修改CAD捕捉时,Z值对捕捉不准的影响3. 修改图片对象选择后,自动跑 ...

  6. 抓取猫眼电影top100的正则、bs4、pyquery、xpath实现方法

    import requests import re import json import time from bs4 import BeautifulSoup from pyquery import ...

  7. 巩固JavaSE基础--IDEA完成实战项目

    PS:学习完JavaSE基础后,需要有一个项目来测试自己的学习成果,并加以巩固.所以在这里,就让我们来学习下“一本糊涂账”项目吧.(此项目来源于Java自学网站) 项目完成效果图一览

  8. try catch影响Spring事务吗?

    对于这个问题有两种情况: 1.catch只打印异常,不抛出异常 try { 数据库做添加订单表; /; 数据库减少库存; }catch (Exception e){ e.printStackTrace ...

  9. buf.readUInt32BE()函数详解

    buf.readUInt32BE(offset[, noAssert]) buf.readUInt32LE(offset[, noAssert]) offset {Number} 0 noAssert ...

  10. python+pyqt5实现24点小游戏

    本文实例为大家分享了python实现24点游戏的具体代码,供大家参考,具体内容如下 描述:一副牌中A.J.Q.K可以当成是1.11.12.13.任意抽取4张牌,用加.减.乘.除(可加括号)把牌面上的数 ...