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. thinkPHP中_initialize方法实例分析

    子类的_initialize方法自动调用父类的_initialize方法. 而php的构造函数construct,如果要调用父类的方法,必须在子类构造函数显示调用parent::__construct ...

  2. Yourphp  使用说明

    https://wenku.baidu.com/view/c8d2e667cc1755270722088a.html 这个是站点的配置信息,比如:网站名称. LOGO .网站标题等 推荐位:个别可能用 ...

  3. Linux初学者必知的5个学习网站

    分享几个Linux初学者一定要知道的5个学习网站 工具/原料 有一颗学习Linux的心 电脑 方法/步骤 1 推荐一:鸟哥的Linux私房菜(http://vbird.dic.ksu.edu.tw/) ...

  4. 如何判断NSDictionary是否包含某个键

    方法一: if([[dictionary allKeys] containsObject:key){     // contains key} 方法二: if([dictionary objectFo ...

  5. 【开发技术】对文件内容进行加密-java

    http://hi.baidu.com/java0804ms/item/111ea834fbd4d2f596f88d5a 实现效果:对文件内容进行加密,使之直接打开成为乱码,不以明文显示 实现步骤:1 ...

  6. 开源三维地球GIS引擎Cesium常用功能的开发

    Cesium是一个非常优秀的三维地球GIS引擎(开源且免费).能够加载各种符合标准的地图图层,瓦片图.矢量图等都支持.支持3DMax等建模软件生成的obj文件,支持通用的GIS计算:支持DEM高程图. ...

  7. jQuery的鼠标事件总结

    jQuery的鼠标事件总结 1.click()事件. 2.dbclick()鼠标双击事件 3.mousedown()鼠标按下事件 4.mouseup()鼠标松开事件 5.mouseover()从一个元 ...

  8. Linux - 在Ubuntu下永久修改主机名

    查看主机名 root@jiqing:~# hostname jiqing 1.临时生效 root@jiqing:~# hostname jq root@jiqing:~# hostname jq 重新 ...

  9. iris数据集 决策树实现分类并画出决策树

    # coding=utf-8 import pandas as pd from sklearn.model_selection import train_test_split from sklearn ...

  10. scrapy_移除内容中html标签

    如何移除所获取内容中多余的html标签? 通过w3lib模块和re模块 #!/usr/bin/python3 # -*- coding: UTF-8 -*- __author__ = 'beimenc ...