ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode每日一题——690. 员工的重要性

2021-05-01 15:36:22  阅读:203  来源: 互联网

标签:690 get int employees id public 重要性 LeetCode size


题目描述

给定一个保存员工信息的数据结构,它包含了员工 唯一的 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, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
输出:11
解释:
员工 1 自身的重要度是 5 ,他有两个直系下属 2 和 3 ,而且 2 和 3 的重要度均为 3 。因此员工 1 的总重要度是 5 + 3 + 3 = 11 。

提示:

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

朴素递归

 1 /*
 2 // Definition for Employee.
 3 class Employee {
 4     public int id;
 5     public int importance;
 6     public List<Integer> subordinates;
 7 };
 8 */
 9 
10 class Solution {
11     public int getImportance(List<Employee> employees, int id) {
12         int size = employees.size();
13 
14         if (size == 1) {
15             return employees.get(0).importance;
16         }
17 
18         return dfs(id, employees);
19     }
20 
21     public int dfs(int parent, List<Employee> employees) {
22         Employee parEmp = null;
23         // 找到父亲
24         for (int i = 0; i < employees.size(); i++) {
25             if (employees.get(i).id == parent) {
26                 parEmp = employees.get(i);
27                 break;
28             }
29         }
30 
31         int sum = parEmp.importance;
32 
33         // 获取直系下属的id集合
34         List<Integer> sub = parEmp.subordinates;
35 
36         for (int i = 0; i < sub.size(); i++) {
37             sum += dfs(sub.get(i), employees);
38         }
39 
40         return sum;
41     }
42 }

提交反馈

递归+Map实现

 1 /*
 2 // Definition for Employee.
 3 class Employee {
 4     public int id;
 5     public int importance;
 6     public List<Integer> subordinates;
 7 };
 8 */
 9 
10 class Solution {
11     public int getImportance(List<Employee> employees, int id) {
12         int size = employees.size();
13 
14         if (size == 1) {
15             return employees.get(0).importance;
16         }
17 
18         Map<Integer, Integer> map = getMap(employees);
19 
20         return dfs(id, employees, map);
21     }
22 
23     // 开辟一个哈希表,通过线性O(n)的复杂度先预处理好,方便每次递归获取上级领导,即不用每次递归还要花费O(n)的线性复杂度去拿到上级领导对象
24     public Map<Integer, Integer> getMap(List<Employee> employees) {
25         Map<Integer, Integer> map = new HashMap<>();
26         for (int i = 0; i < employees.size(); i++) {
27             map.put(employees.get(i).id, i);
28         }
29 
30         return map;
31     }
32 
33     public int dfs(int parent, List<Employee> employees, Map<Integer, Integer> map) {
34         Employee parEmp = employees.get(map.get(parent));
35 
36         int sum = parEmp.importance;
37 
38 
39         // 获取直系下属的id集合
40         List<Integer> sub = parEmp.subordinates;
41 
42         for (int i = 0; i < sub.size(); i++) {
43             sum += dfs(sub.get(i), employees, map);
44         }
45 
46         return sum;
47     }
48 }

提交反馈

 

BFS +Map

 1 /*
 2 // Definition for Employee.
 3 class Employee {
 4     public int id;
 5     public int importance;
 6     public List<Integer> subordinates;
 7 };
 8 */
 9 
10 class Solution {
11     public int getImportance(List<Employee> employees, int id) {
12         int size = employees.size();
13 
14         if (size == 1) {
15             return employees.get(0).importance;
16         }
17 
18         // 额外开一个哈希表,用于记录员工对应的下标,方便后面直接O(1)时间获取
19         Map<Integer, Integer> map = getMap(employees);
20 
21         // bfs
22         Deque<Employee> deque = new LinkedList<>();
23         deque.add(employees.get(map.get(id)));
24         
25         int sum = 0;
26         while (deque.size() > 0) {
27             // 弹出
28             Employee parent = deque.poll();
29             List<Integer> subList = parent.subordinates;
30             sum += parent.importance;
31             // 遍历所有下属
32             for (int subId : subList) {
33                 Employee subEmp = employees.get(map.get(subId));
34                 // 将下属进队列
35                 deque.add(subEmp);
36             }
37         }
38 
39         return sum;
40     }
41 
42     public Map<Integer, Integer> getMap(List<Employee> employees) {
43         Map<Integer, Integer> map = new HashMap<>();
44         for (int i = 0 ; i < employees.size(); i++) {
45             // 将员工编号id作为map的键,员工所在集合的索引下标作为值
46             map.put(employees.get(i).id, i);
47         }
48         return map;
49     }
50 }

提交反馈

 

标签:690,get,int,employees,id,public,重要性,LeetCode,size
来源: https://www.cnblogs.com/pengsay/p/14724211.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有