LeetCode 690. Employee Importance (职员的重要值)

时间:2021-09-21 23:41:50

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.

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.

题目标签:HashTable

  题目给了我们一个 employees list, 和 一个 id,让我们找到这个 id 的员工的手下所有员工的 importance 累加,包括他自己的。

  首先把 employees 存入 HashMap, id 为 key, Employee 为value。

  然后建立一个 dfs function:

    当员工的 subordinates 的 size  等于 0 的时候, 说明没有必要继续递归了,返回员工的重要值;

    如果 size 大于0,那么遍历 subordinates,把每一个 员工id 递归,累加重要值。

Java Solution:

Runtime beats 71.9%

完成日期:11/16/2017

关键词:HashMap, DFS

关键点:把employee 信息存入map,id 为 key,employee 为 value,便于dfs 直接调取 员工信息

 /*
// 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)
{
HashMap<Integer, Employee> map = new HashMap<>(); for(Employee e: employees)
map.put(e.id, e); return dfs(id, map);
} private int dfs(int id, HashMap<Integer, Employee> map)
{
Employee e = map.get(id); if(e.subordinates.size() == 0)
return e.importance; int imp = e.importance; for(int sub: e.subordinates)
imp += dfs(sub, map); return imp;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 690. Employee Importance (职员的重要值)的更多相关文章

  1. &lpar;BFS&rpar; leetcode 690&period; Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  2. LN &colon; 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 ...

  4. LeetCode - 690&period; Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  5. leetcode 690&period; Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  6. &lbrack;LeetCode&rsqb;690&period; Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  7. 【Leetcode&lowbar;easy】690&period; Employee Importance

    problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...

  8. 【LeetCode】690&period; Employee Importance 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...

  9. &lbrack;LeetCode&amp&semi;Python&rsqb; Problem 690&period; Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

随机推荐

  1. windows bat批处理语法简析

    第一节先介绍windows批处理.这个起源于跟旁边同事学习在windows用命令行办公,渐渐地有些批处理功能就需要了,于是专门抽出了几天学习了一下.我认为文档最重要的功能是为了备忘,择取了很多文档的例 ...

  2. ASP&period;NET MVC 多语言实现技巧 最简、最易维护和最快速开发

    说说传统做法的缺点 1.做过多语言的都知道这玩意儿太花时间 2.多语言架构一般使用资源文件.XML或者存储数据库来实现.这样就在一定程序上降低了性能 3.页面的可读性变差,需要和资源文件进行来回切换 ...

  3. QListWidget

    1.失去焦点背景颜色,代码设置全选的时候,背景会是白色,需要设置失去焦点背景颜色.(设置焦点,会出现白转化成设置背景色,效果不好) QPalette p; p.setColor(QPalette::I ...

  4. C陷阱与缺陷 2

    1,数组 对数组只能进行两种操作,1确定数组的大小,2获得数组第一个元素的指针,其他的操作均是通过指针来实现的. 1 2 3 4 5 6 7 8 9 #include <stdio.h> ...

  5. servlet中的cookie

    cookie的机制是:从客户端(浏览器)发送请求到服务器,然后服务器把接受的信息回写到客户端,这个信息在客户端跟服务器之间进行交互. 下面是一个创建cookie的小案例 //如何创建cookie pa ...

  6. 好玩的WPF第二弹:电子表字体显示时间&plus;多彩呼吸灯特效button

    我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单很多. 首先是定义一个TextBlock例如以下. <Grid> <TextBlock Name=& ...

  7. 举例详解Python中的split&lpar;&rpar;函数的使用方法

    这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下   函数:sp ...

  8. springMVC初探

    MVC框架要做哪些事情? a,将url映射到java类,或者java类的方法上 b,封装用户提交的数据 c,处理请求->调用相关的业务处理—>封装响应的数据 d,将响应的数据进行渲染 sp ...

  9. django搭建web &lpar;三&rpar; admin&period;py -- 待续

    demo 关于模型myQuestion,myAnswer将在后述博客提及 # -*- coding: utf-8 -*- from __future__ import unicode_literals ...

  10. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...