LeetCode 690. Employee Importance 员工的重要性(C++/Java)
题目:
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:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won't exceed 2000.
分析:
有一个保存员工信息的数据结构,包含id和重要度,以及一个其下属的id列表。现在给定一个这样的员工数组,求员工和其所有下属的重要度之和。
首先遍历数组,将员工的id和其对应的员工存入map中,方便我们直接获取到它的信息。
然后深度优先搜索,从所给的id开始求,递归求解他们的和即可。也可以使用bfs,将每一个员工的下属加入到队列中,将重要度累加,知道队列为空即可。
程序:
C++
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> m;
for(auto em:employees){
m.insert({em->id, em});
}
return dfs(id, m);
}
private:
int dfs(int id, unordered_map<int, Employee*> &m){
int sum = m[id]->importance;
for(auto i:m[id]->subordinates){
sum += dfs(i, m);
}
return sum;
}
};
Java
/*
// 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> m = new HashMap<>();
for(Employee e:employees){
m.put(e.id, e);
}
return dfs(id, m);
}
private int dfs(int id, HashMap<Integer, Employee> m){
int sum = m.get(id).importance;
for(Integer i:m.get(id).subordinates)
sum += dfs(i, m);
return sum;
}
}
LeetCode 690. Employee Importance 员工的重要性(C++/Java)的更多相关文章
- [LeetCode]690. Employee Importance员工重要信息
哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- Leetcode690.Employee Importance员工的重要性
给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结构是[1 ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
随机推荐
- steam社区留言红小作文模板
steam社区留言红小作文模板 Dear steam: Im a steam user which most play csgo.i saw i had be banned in steam comm ...
- 云原生数据仓库TPC-H第一背后的Laser引擎大揭秘
简介: 作者| 魏闯先阿里云数据库资深技术专家 一.ADB PG 和Laser 计算引擎的介绍 (一)ADB PG 架构 ADB PG 是一款云原生数据仓库,在保证事务ACID 能力的前提下,主要解决 ...
- 实时计算 Flink 版总体介绍
简介: 实时计算 Flink 版(Alibaba Cloud Realtime Compute for Apache Flink,Powered by Ververica)是阿里云基于 Apache ...
- 贾扬清演讲实录:一个AI开发者的奇幻漂流
简介:2021阿里灵杰AI工程化峰会,贾扬清深度解读阿里灵杰大数据和AI一体化平台. 演讲人:贾扬清 演讲主题:一个AI开发者的奇幻漂流 活动:2021阿里灵杰AI工程化峰会 对于绝大多数人来说,这 ...
- Apache Dubbo 3.0.0 正式发布 - 全面拥抱云原生
简介: 一个新的里程碑! 一.背景 自从 Apache Dubbo 在 2011 年开源以来,在一众大规模互联网.IT公司的实践中积累了大量经验后,Dubbo 凭借对 Java 用户友好.功能丰富.治 ...
- 【产品动态】解读Dataphin流批一体的实时研发
简介: Dataphin作为一款企业级智能数据构建与管理产品,具备全链路实时研发能力,从2019年开始就支撑可集团天猫双11的实时计算需求,文章将详细介绍Dataphin实时计算的能力. 背景 每当 ...
- Serverless 工程实践 | Serverless 应用开发观念的转变
简介: Serverless 架构带来的除了一种新的架构.一种新的编程范式,还包括思路上的转变,尤其是开发过程中的一些思路转变.有人说要把 Serverless 架构看成一种天然的分布式架构,需要用 ...
- 在线运行代码的 PHP 沙盒环境实现
演示: 多版本PHP运行代码 作用: 方便作为独立的调试环境运行 一些 临时逻辑,查看执行结果. 方便比较不同版本的 PHP 执行差异,进行一般的兼容性测试. 思路: Docker镜像构建多个PH ...
- .Net 线程与锁
一台服务器能运行多少个线程,大致取决于CPU的管理能力.CPU负责线程的创建.协调.切换.销毁.暂停.唤醒.运行等.一个应用程序中,必须有一个进程维持应用程序的运行环境,一个进程可同时有多个线程协作处 ...
- PCI-E与SATA SSD
为什么要采用PCI-E通道 目前在固态硬盘SSD中,有一部分采用了SATA3.0接口,而一些高端的固态硬盘则采用了PCI-E接口.那么为什么高端固态硬盘要采用PCI-E接口呢?为了弄清楚这个问题,先看 ...