为有牺牲多壮志,敢教日月换新天。

[Java]LeetCode690. 员工的重要性 | Employee Importance

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10622848.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' 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:

  1. One employee has at most one direct leader and may have several subordinates.
  2. The maximum number of employees won't exceed 2000.

给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id。

比如,员工1是员工2的领导,员工2是员工3的领导。他们相应的重要度为15, 10, 5。那么员工1的数据结构是[1, 15, [2]],员工2的数据结构是[2, 10, [3]],员工3的数据结构是[3, 5, []]。注意虽然员工3也是员工1的一个下属,但是由于并不是直系下属,因此没有体现在员工1的数据结构中。

现在输入一个公司的所有员工信息,以及单个员工id,返回这个员工和他所有下属的重要度之和。

示例 1:

输入: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
输出: 11
解释:
员工1自身的重要度是5,他有两个直系下属2和3,而且2和3的重要度均为3。因此员工1的总重要度是 5 + 3 + 3 = 11。

注意:

  1. 一个员工最多有一个直系领导,但是可以有多个直系下属
  2. 员工数量不超过2000。

4ms

 1 /*
 2 // Employee info
 3 class Employee {
 4     // It's the unique id of each node;
 5     // unique id of this employee
 6     public int id;
 7     // the importance value of this employee
 8     public int importance;
 9     // the id of direct subordinates
10     public List<Integer> subordinates;
11 };
12 */
13 class Solution {
14     HashMap<Integer,Employee> h;
15     public int getImportance(List<Employee> employees, int id) {
16         h = new HashMap<Integer,Employee>();
17         for(Employee emp: employees)
18             h.put(emp.id,emp);
19         return dfs(id);
20     }
21     
22     public int dfs(int id){
23         Employee e = h.get(id);
24         int ans = e.importance;
25         for(Integer sub:e.subordinates)
26             ans += dfs(sub);
27         return ans;
28     }
29 }

5ms

 1 class Solution {
 2     
 3     private int getSum(HashMap<Integer, Employee> map, int id){
 4         int sum = map.get(id).importance;
 5         for(int sub : map.get(id).subordinates){
 6             sum += getSum(map, sub);
 7         }
 8         return sum;
 9     }
10     
11     public int getImportance(List<Employee> employees, int id) {
12         HashMap<Integer, Employee> map = new HashMap<Integer, Employee>();
13         
14         for(Employee e : employees){
15             map.put(e.id, e);
16         }
17         
18         return getSum(map, id);
19     }
20 }

6ms

 1 class Solution {
 2     public int getImportance(List<Employee> employees, int id) {
 3         HashMap<Integer, Integer> map = new HashMap<>();
 4         HashMap<Integer, List<Integer>> rela = new HashMap<>();
 5         for (Employee emp : employees) {
 6             rela.put(emp.id, emp.subordinates);
 7             map.put(emp.id, emp.importance);
 8         }
 9         int sum = 0;
10         List<Integer> list = new ArrayList<>();
11         list.add(id);
12         while (list.size() > 0) {
13             List<Integer> next = new ArrayList<>();
14             for (int i : list) {
15                 sum += map.get(i);
16                 if (rela.containsKey(i)) {
17                     next.addAll(rela.get(i));
18                 }
19             }
20             list = next;
21         }
22         return sum;
23     }
24 }

 

posted @ 2019-03-29 17:44  为敢技术  阅读(294)  评论(0编辑  收藏  举报