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. 将本地项目或代码上传到别人GitHub(码云)的远程分支上

    今天碰到了这样一个问题,折腾了半天,就是将自己本地代码上传到人家的远程分支上. 首先要做的就是先将人家的项目克隆到本地:git clone + 项目地址 然后进入项目目录:cd + 已克隆好的项目目录 ...

  2. 《并行程序设计导论》——OpenMP

    OpenMP看着很好,实际上坑很多. 如果真的要求性能和利用率,还是专门写代码吧.而且MS的VS里只有2.X的版本.

  3. 【开发技术】Eclipse插件Call Hierarchy简介及设置

    Call Hierarchy 主要功能是 显示一个方法的调用层次(被哪些方法调,调了哪些方法) 在MyEclipse里Help - Software updates - Find and instal ...

  4. [转]另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

    转自: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这个 ...

  5. python判断两个list包含关系

    a = [1,2] b = [1,2,3] c = [0, 1] set(b) > set(a) set(b) > set(c)

  6. python3 第六章 - 条件判断

    Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 条件语句的执行过程,如下图: 条件语句,又称为if语句,它的完整语法如下: if 条件1: 语句块1 ...

  7. logback使用配置详解

    title: logback使用配置详解 date: 2017-04-25 16:42:49 tags: 日志 --- 1.介绍 Logback是由log4j创始人设计的另一个开源日志组件,它当前分为 ...

  8. secureCRT sftp使用

    sftp-- help 可用命令: cd 路径 更改远程目录到"路径" lcd 路径 更改本地目录到"路径" chgrp group path 将文件" ...

  9. java时间格式转化(毫秒 to 00:00)

    把秒数转换为%d:%02d:%02d 格式 private String stringForTime(int timeSec) { int totalSeconds = timeSec; int se ...

  10. jsp的语法

    JSP指令和脚本元素指令 <%@ 指令%>声明 <%! 声明%>表达式 <%= 表达式%>代码段/脚本段 <% 代码段%>注释 <%-- 注释-- ...