[LeetCode] 690. Employee Importance

You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.

You are given an array of employees employees where:

  • employees[i].id is the ID of the ith employee.
  • employees[i].importance is the importance value of the ith employee.
  • employees[i].subordinates is a list of the IDs of the direct subordinates of the ith employee.

Given an integer id that represents an employee's ID, return the total importance value of this employee and all their direct and indirect subordinates.

Example 1:

Input: employees = [[1,5,[2,3]],[2,3,[]],[3,3,[]]], id = 1
Output: 11
Explanation: Employee 1 has an importance value of 5 and has two direct subordinates: employee 2 and employee 3.
They both have an importance value of 3.
Thus, the total importance value of employee 1 is 5 + 3 + 3 = 11.

Example 2:

Input: employees = [[1,2,[5]],[5,-3,[]]], id = 5
Output: -3
Explanation: Employee 5 has an importance value of -3 and has no direct subordinates.
Thus, the total importance value of employee 5 is -3.

Constraints:

  • 1 <= employees.length <= 2000
  • 1 <= employees[i].id <= 2000
  • All employees[i].id are unique.
  • -100 <= employees[i].importance <= 100
  • One employee has at most one direct leader and may have several subordinates.
  • The IDs in employees[i].subordinates are valid IDs.

员工重要性。

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/employee-importance
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道图论graph的题,不难,搞懂题意就能做的出来。题目给的是所有员工的id,重要度和他们知悉下属的id。既然能拿到当前员工的id,那么拿到当前员工的重要度和他的下属也就不是问题,所以无论是什么做法,我们都需要一个hashmap记住所有员工的重要度和他们各自的下属。至于做法方面,我们可以用BFS或DFS,做法都跟一般的BFS和DFS类型的题非常类似,看了代码应该能懂。

BFS

时间O(V + E)

空间O(V) - number of employees

Java实现

 1 class Solution {
 2     public int getImportance(List<Employee> employees, int id) {
 3         int total = 0;
 4         HashMap<Integer, Employee> map = new HashMap<>();
 5         for (Employee employee : employees) {
 6             map.put(employee.id, employee);
 7         }
 8         Queue<Employee> queue = new LinkedList<>();
 9         queue.offer(map.get(id));
10         while (!queue.isEmpty()) {
11             Employee cur = queue.poll();
12             total += cur.importance;
13             for (int subordinate : cur.subordinates) {
14                 queue.offer(map.get(subordinate));
15             }
16         }
17         return total;
18     }
19 }

 

DFS

时间O(V + E)

空间O(V) - number of employees

Java实现

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

 

相关题目

339. Nested List Weight Sum

364. Nested List Weight Sum II

690. Employee Importance

LeetCode 题目总结

posted @ 2020-08-21 01:06  CNoodle  阅读(176)  评论(0编辑  收藏  举报