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.
/*
// 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) {
return get(employees, id);
} private int get(List<Employee> employees, int id) {
int total = 0;
for (Employee e : employees) {
if (id == e.id) {
total = e.importance;
for (int subId : e.subordinates) {
total += get(employees, subId);
}
break;
}
}
return total;
}
}

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

  4. LeetCode 690 Employee Importance 解题报告

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

  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. 云计算之路-阿里云上:节点 CPU 波动引发 docker swarm 集群故障

    非常抱歉,今天 10:05-10:20 左右,我们用阿里云服务器搭建的 docker swarm 集群又出现故障,又是因为突然的节点 CPU 波动. 受这次故障影响的站点有 闪存,博问,班级,园子,短 ...

  2. maven项目 在eclipse,InteliJ IDEA中的一些问题

    转载请注明出处,谢谢! 不论我们用什么ide来编辑我们的代码,最终的产品都会脱离ide来运行:正如燕飞离了巢,正如你离开了家,不期然就会运转出现问题. - 单强 2018年1月26日11:53 大家是 ...

  3. TPshop学习(8)微信支付

    http://blog.csdn.net/phper8/article/details/76383415 学习内容: https://www.kancloud.cn/tpshop/thinkphp5/ ...

  4. PHP 正则表达式匹配函数 preg_match 与 preg_match_all

    preg_match() preg_match() 函数用于进行正则表达式匹配,成功返回 1 ,否则返回 0 . 语法: 1 int preg_match( string pattern, strin ...

  5. 安装mysql后运行.net程序出错

    安装mysql后运行.net程序出错: 出错位置:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config  出错信息:未 ...

  6. CCF系列之最大的矩形(201312-3)

    试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩 ...

  7. jq dom操作

    下面介绍我在网络上找的JQUERY的父,子,兄弟节点查找方法 jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent() ...

  8. Java this 关键字的用法

    this 关键字的用法 this 在类中就是代表当前对象,可以通过 this 关键字完成当前 对象的成员属性.成员方法和构造方法的调用. 那么何时用 this? 当在定义类中的方法时,如果需要调用该类 ...

  9. python_继承supper错误

    问题: qs = super(BnnerCourseAdmin, self).queryset() TypeError: super(type, obj): obj must be an instan ...

  10. Linux磁盘热插拔命令

    对于支持热插拔SCSI技术的服务器,SCSI硬盘和磁带机可以在Linux下实现在线添加和移除,有两种方法实现,先说麻烦的 具体方法如下: 一. 添加或者删除硬盘/磁带机: # echo "s ...