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 ...
随机推荐
- iOS中ARC和非ARC混用
如果在使用第三方类库的时候,我们可能会遇到一些内存管理的问题 那么如何在一个工程中实现ARC和非ARC混用呢,例如你创建一个ARC的工程,但是你引用的第三方类库是非ARC管理内存的 首先点击工 ...
- poi获取word批注
package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...
- Oracle+struts2实现用户登入并显示访问次数
实体类: package entity; public class userfo { private int id;//id private String name;//用户名 private Str ...
- 系统信号-signal.h
#define SIGSEGV 11 /* segmentation violation */ #define SIGSYS 12 /* bad argument to system call */ ...
- 探索 DWARF 调试格式信息
https://www.ibm.com/developerworks/cn/aix/library/au-dwarf-debug-format/ 简介 DWARF(使用有属性的记录格式进行调试 )是许 ...
- @EnableConfigurationProperties
参考:https://www.jianshu.com/p/7f54da1cb2eb 使用 @ConfigurationProperties 注解的类生效. 如果一个配置类只配置@Configurati ...
- 干货分享--iOS及Mac开源项目和学习资料【超级全面】
原文出处:codecloud http://www.kancloud.cn/digest/ios-mac-study/84557
- css--小白入门篇1
一.引入 css用来描述html,学习css前我们先来学习html的基础标签的用法,再进入css的学习. 本教程面向小白对象,不会讲细枝末节深入的东西. 二.列表 列表有3种 2.1 无序列表 无序列 ...
- 洛谷——P2158 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 找规律大水题嘛,如果你做过P1170 兔八哥与猎人 这题得到的规律是$a,b,c,d$,若$gcd(a-b,c-d)==1$ 那么$a,b$就能看到$c,d$ ...
- 「 poj 2096 」 Collecting Bugs
先说一下题意 $s$ 个子系统还中有 $n$ 种 $\text{bug}$,每天可以随机选择一种 $\text{bug}$,问选出 $n$ 种 $\text{bug}$ 在 $s$ 种子系统中的期望天 ...