LeetCode 690. Employee Importance (职员的重要值)
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:
- One employee has at most one direct leader and may have several subordinates.
- 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 (职员的重要值)的更多相关文章
- (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 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [LeetCode]690. Employee Importance员工重要信息
哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- 【LeetCode】690. Employee Importance 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...
随机推荐
- re.S解析
转自:https://www.cnblogs.com/xieqiankun/p/re-sinpython.html 在Python的正则表达式中,有一个参数为re.S.它表示“.”(不包含外侧双引号, ...
- OC语言Block
OC语言Block 一.Block (一)简介 Block是什么?苹果推荐的比较特殊的数据类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. Block和 ...
- spark查看stage和tasks信息
spark提供了web-ui接口.外部命令等多种方法监视spark程序的执行状态.利用spark的监视功能,可以方便的查看spark应用程序执行的状态,具体包括:1)stage和tasks列表信息 ...
- Python的伪造数据库:Faker
faker 是一个可以让你生成伪造数据的Python包,在软件需求.开发.测试过程中常常需要利用一些假数据来做测试,这种时候就可以使用 Faker 来伪造数据从而用来测试. 一.Faker安装 pip ...
- Jmeter之重定向请求
一.自动重定向和跟随重定向的区别 自动重定向:状态码一般是200.20X.当重定向自动跳转时,只针对GET和Head请求,自动重定向自动跳转到最终目标页面,当HTTP请求为自动重定向时,JMeter不 ...
- 06Microsoft SQL Server 完整性约束
Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...
- uploadify的简单使用
简单的图片上传: 1.进入官网下载uploadify插件:http://www.uploadify.com/download/ 2.导入uploadify插件提供的css样式和类库: <link ...
- gearman的安装与使用
Gearman是一个分发任务的程序框架,它会对作业进行排队自动分配到一系列机器上.gearman跨语言跨平台,很方便的实现异步后台任务.php官方收录:http://php.net/manual/zh ...
- 【转载】Spring注解@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...
- MyBatis 的基本要素—SQL 映射文件
MyBatis 真正的强大在于映射语句,相对于它强大的功能,SQL 映射文件的配置却是相当简单.对比 SQL 映射配置和 JDBC 代码,发现使用 SQL 映射文件配置可减少 50% 以上的代码,并且 ...