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 ...
随机推荐
- c#程序员必学清单
必读书目:1. "Effective C#: 50 Specific Ways to Improve Your C#" by Bill Wagner2. "CLR via ...
- stm32串口晶振不对输出乱码+汇承HC-14lora模块
最近要用到一个lora无线透传模块,然后就先用两个32开发板(用的STM32F103C8T6)试试简单的收发数据.结果,第一步串口发送一句话就寄了,我串口打印了"hi",结果出现了 ...
- JVM简明笔记3:类加载机制
1 类的加载 类的加载指的是将类的 .class 文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个 java.lang.Class 对象,用来封装类在方法区内的数据结 ...
- Dubbo Mesh:从服务框架到统一服务控制平台
简介: Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java.Golang 等多语言 SDK 实现. 作者:Dubbo 社区 Ap ...
- 巧用API网关构建大型应用体系架构
简介: 近期阿里云重磅发布了BizWorks一体化的云原生应用的开发和运营平台,内置阿里巴巴业务中台构建的最佳技术实践.它已经将API网关作为关键组件融入其中,并且基于API网关为用户提供能力开放平台 ...
- 性能提升 57% ,SMC-R 透明加速 TCP 实战解析 | 龙蜥技术
简介:SMC-R 是如何加速 TCP 应用? 编者按:TCP 协议作为当前使用最为广泛的网络协议,场景遍布移动通信.数据中心等.对于数据中心场景,通过弹性 RDMA 实现高性能网络协议 SMC-R, ...
- 重磅发布 阿里云数据中台全新产品DataTrust聚焦企业数据安全保障
简介: DataTrust(隐私增强计算产品)是基于阿里云底层多项基础安全能力,经过阿里云数据中台丰富的客户业务实践,构建的一款为企业数据安全流通的产品. 随着包括零售.制造.金融等多行业数字化转型加 ...
- 最强AI直播换脸软件,DeepFaceLive下载介绍
DeepFaceLive是一款专注于直播实时换脸的AI软件,使用经过长时间训练的人脸模型替换摄像头中的人脸,能够产生接近电影质量的面部合成效果,提供高保真的视觉体验,在新版本中也支持了图片换脸(视频换 ...
- 建立成功平台工程的关键:自助式 IaC
从技术上讲,云一直都是自助式服务,但由于其在实践中的复杂性,许多开发人员并不喜欢.随着公司采用现代架构(云原生.无服务器等)和新的提供商(多云.SaaS 应用程序),以及云提供商发布更多服务,云变得更 ...
- 使用WebSocket实现实时多人答题对战游戏
前言 前两章教程,我们使用WebSocket的基础特性打造了一个小小聊天室,并在第二章对其进行了集群化改造. 系列教程回顾: [WebSocket]第一章:手把手搭建WebSocket多人在线聊天室( ...