这是悦乐书的第291次更新,第309篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第159题(顺位题号是690)。定义员工信息的数据结构,其中包括员工的唯一ID,他的重要性值以及他的直接下属ID。例如,员工1是员工2的领导者,员工2是员工3的领导者。他们的重要性值分别为15,10和5。然后,员工1具有[1,15,[2]]等数据结构,员工2具有[2,10,[3]],员工3具有[3,5,[]]。请注意,虽然员工3也是员工1的下属,但该关系不是直接的。现在,根据公司的员工信息和员工ID,您需要返回该员工及其所有下属的总重要性值。

输入:[[1,5,[2,3]],[2,3,[]],[3,3,[]]],1

输出:11

说明:员工1具有重要性值5,他有两个直接下属:员工2和员工3.他们都具有重要性值3.因此员工1的总重要性值是5 + 3 + 3 = 11。

注意:

  • 一名员工最多只有一名直接领导,但是可以有多名下属。

  • 最多员工人数不超过2000人。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

题目的意思是找到所给id员工的重要性以及所有下属员工重要性之和,如果其下属员工本身也有下属,也要算进去,也就是说该问题的子问题还是问题本身。因此我们可以使用递归的方法。

先找到当前id所在的员工信息和其下属员工的id集合,我们将其下属员工id放入HashSet中,然后再去遍历所有员工集合,如果HashSet中包含当前员工的id,就累加上其重要值,然后对其下属员工进行再处理,调用递归方法,最后返回sum。

/*
// 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 helper(employees, id);
} public int helper(List<Employee> employees, int id){
List<Integer> sub = null;
int sum = 0;
for (Employee em : employees) {
if (em.id == id) {
sub = em.subordinates;
sum += em.importance;
break;
}
}
Set<Integer> set = new HashSet<Integer>();
for (Integer num : sub) {
set.add(num);
}
for (Employee em : employees) {
if (set.contains(em.id)) {
sum += helper(employees, em.id);
}
}
return sum;
}
}

03 第二种解法

我们可以对第一种解法再优化下,依旧使用递归,但是使用HashMap对初始员工数据进行处理,以id为key,以员工信息为value。递归方法中,以id为参数,查找当前id在HashMap中对应的员工信息,定义一个sum变量,初始值为当前员工的重要性,遍历当前员工的下属员工id集合,调用递归方法本身,最后返回sum。

/*
// 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 Map<Integer, Employee> map ;
public int getImportance(List<Employee> employees, int id) {
map = new HashMap<Integer, Employee>();
for (Employee em : employees) {
map.put(em.id, em);
}
return helper(id);
} public int helper(int id){
Employee em = map.get(id);
int sum = em.importance;
for (Integer num : em.subordinates) {
sum += helper(num);
}
return sum;
}
}

04 第三种解法

我们也可以使用迭代的方式来处理,借助队列来实现,当然你也可以使用栈。依旧是将所有的员工信息遍历放入HashMap中,以id为key,以员工信息为value。然后创建一个队列,将参数id作为初始元素入队列,接着对队列中的数据进行处理,出队列,然后使用id在HashMap中查找对应的value,找到后,累加上其重要值,然后遍历该员工的下属,将下属的id加入到队列中去,一直循环处理,直到队列为空。最后返回sum。

/*
// 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) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
for (Employee em : employees) {
map.put(em.id, em);
}
int sum = 0;
Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(id);
while (!queue.isEmpty()) {
int temp = queue.poll();
Employee e = map.get(temp);
sum += e.importance;
for (Integer num : e.subordinates) {
queue.offer(num);
}
}
return sum;
}
}

05 小结

算法专题目前已日更超过四个月,算法题文章159+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Employee Importance(Java实现)的更多相关文章

  1. LeetCode算法题-Heaters(Java实现)

    这是悦乐书的第239次更新,第252篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第106题(顺位题号是475).冬天来了!您在比赛期间的第一份工作是设计一个固定温暖半径 ...

  2. LeetCode算法题-Sqrt(Java实现)

    这是悦乐书的第158次更新,第160篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第17题(顺位题号是69). 计算并返回x的平方根,其中x保证为非负整数. 由于返回类型 ...

  3. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  4. LeetCode算法题-Number of Lines To Write String(Java实现)

    这是悦乐书的第319次更新,第340篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第188题(顺位题号是806).我们要将给定字符串S的字母从左到右写成行.每行最大宽度为 ...

  5. LeetCode算法题-Unique Morse Code Words(Java实现)

    这是悦乐书的第318次更新,第339篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第186题(顺位题号是804).国际莫尔斯电码定义了一种标准编码,其中每个字母映射到一系 ...

  6. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  7. LeetCode算法题-Rotated Digits(Java实现)

    这是悦乐书的第316次更新,第337篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第185题(顺位题号是788).如果一个数字经过180度旋转后,变成了一个与原数字不同的 ...

  8. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

  9. LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

    这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...

随机推荐

  1. RAC集群数据库连库代码示例(jdbc thin方式,非oci)

    1.RAC集群数据库连库代码示例(jdbc thin方式,非oci):jdbc.driverClassName=oracle.jdbc.driver.OracleDriverjdbc.url=jdbc ...

  2. 从零开始学习PYTHON3讲义(一)认识Python

    课程名称 从零开始PYTHON3 课程长度 15讲 适用年龄 15-20岁(初三-大一) 本讲名称 认识Python 时长 90分钟 教学内容分析 Python是时下最流行的计算机编程语言之一.本课程 ...

  3. .netcore使用SocketAsyncEventArgs Pool需要注意!

    在.net中做网络通讯往往都会用到SocketAsyncEventArgs,为了得到更好的性能配合Pool复用SocketAsyncEventArgs可以得到一个更好的效果,但在dotnet core ...

  4. Chapter 5 Blood Type——1

    The rest of the morning passed in a blur. 早上剩下的时间都在模糊中度过了. It was difficult to believe that I hadn't ...

  5. Chapter 5 Blood Type——2

    The rest of the morning passed in a blur. 早上剩下的时间都在模糊中度过了. It was difficult to believe that I hadn't ...

  6. 精读《Serverless 给前端带来了什么》

    1. 引言 Serverless 是一种 "无服务器架构",让用户无需关心程序运行环境.资源及数量,只要将精力 Focus 到业务逻辑上的技术. 现在公司已经实现 DevOps 化 ...

  7. [十二]基础数据类型之String

    在正式介绍String之前,我们先介绍下CharSequence char + sequence 就是字符的序列的意思 Java中万事万物都是对象类型 而对于字符的序列,也就是多个char, 这么一种 ...

  8. Spring Boot 2.x(十四):整合Redis,看这一篇就够了

    目录 Redis简介 Redis的部署 在Spring Boot中的使用 Redis缓存实战 寻找组织 程序员经典必备枕头书免费送 Redis简介 Redis 是一个开源的使用 ANSI C 语言编写 ...

  9. Pycharm远程调试原理及配置

    工作中使用Pycharm作为python开发的IDE,作为专业的python集成开发环境,其功能之强大令人折服.开发过程中Debug是必不可少的.平时经常使用Pycharm的remote debug功 ...

  10. 第21章 登录 - Identity Server 4 中文文档(v1.0.0)

    为了使IdentityServer能够代表用户发出令牌,该用户必须登录IdentityServer. 21.1 Cookie身份验证 使用由ASP.NET Core中的cookie身份验证处理程序管理 ...