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 ...
随机推荐
- kubernetes CNI(Container Network Inferface)
为什么需要 CNI 在 kubernetes 中,pod 的网络是使用 network namespace 隔离的,但是我们有时又需要互相访问网络,这就需要一个网络插件来实现 pod 之间的网络通信. ...
- Redis介绍、使用、数据结构和集群模式总结
Redis(Remote Dictionary Server)是一个开源的,基于内存的数据结构存储系统,它支持多种数据结构,如字符串(String).列表(List).集合(Set).有序集合(Sor ...
- 力扣122(java&python)-买卖股票的最佳时机 II(中等)
题目: 给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格. 在每一天,你可以决定是否购买和/或出售股票.你在任何时候 最多 只能持有 一股 股票.你也可以先购买 ...
- 流批一体生产应用!Bigo 实时计算平台建设实践
简介: 本文由 Bigo 计算平台负责人徐帅分享,主要介绍 Bigo 实时计算平台建设实践的介绍 本文由 Bigo 计算平台负责人徐帅分享,主要介绍 Bigo 实时计算平台建设实践的介绍.内容包括: ...
- Dubbo-go 优雅上下线设计与实践
简介:在分布式场景下,微服务进程都是以容器的形式存在,在容器调度系统例如 k8s 的支持下运行,容器组 Pod 是 K8S 的最小资源单位.随着服务的迭代和更新,当新版本上线后,需要针对线上正在运行 ...
- [FE] Chrome Extension 五步曲
1. Create the manifest.jsonOnly three fields is needed. { "name": "Getting Started Ex ...
- Fiddler 将插件放在独立子文件夹
我的 Fiddler 安装了许多插件,有一些插件存在 DLL 名冲突问题,比如多个不同的插件都存在名为 PluginCore.dll 但实际实现逻辑完全不相同的程序集.这就导致了多个插件的安装之间,如 ...
- dotnet C# 高性能配置文件读写库 dotnetCampus.Configurations 简介
在应用程序运行的时,需要根据不同的配置执行不同的内容.有很多根据配置而初始化的功能往往是在应用程序启动的时候需要执行.对于很多类型的应用程序,特别是客户端的应用程序,启动的性能特别重要.也因此,在启动 ...
- dotnet C# 调用委托的 GetInvocationList 的对象分配
本文也叫跟着 Stephen Toub 大佬学性能优化系列,这是我从 Stephen Toub 大佬给 WPF 框架做性能优化学到的知识,在热路径下,也就是频繁调用的模块,如果调用了委托的 GetIn ...
- C# - 能否让 SortedSet.RemoveWhere 内传入的委托异步执行
TL;DR; 若想充分利用 RemoveWhere 带来的性能优势,建议传入判断是否删除元素的委托内采取同步操作.若一定要在该委托内使用异步操作,可以采用本文中绕行的方法,但摈弃了 RemoveWhe ...