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 ...
随机推荐
- 剑指offer66(Java)-构建乘积数组(中等)
题目: 给定一个数组 A[0,1,-,n-1],请构建一个数组 B[0,1,-,n-1],其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积, 即 B[i]=A[0]×A[1]×-×A[i ...
- 【Serverless实战】B站每日自动签到&&传统单节点网站的Serverless上云
简介: Serverless好哇!这里将针对个人与生产两个应用方向的测评 使用Serverless实现自动获取每日B站的经验值,让你更快冲到LV6! 你的业务站点还是一台服务器All in One吗? ...
- 赋予企业更多可能,云数据库SQL Server 2019版这几大亮点别错过
直播预告 2020年3月26日 15:00-16:30 邀您一同见证 云数据库SQL Server 2019版重磅发布 全面提升性价比及数据库能力 点我观看 RDS SQL Server 2019不仅 ...
- 【深度学习】基于 Alluxio 数据缓存的性能优化
作者 | 车漾(阿里云高级技术专家).顾荣(南京大学 副研究员) 导读:Alluxio 项目诞生于 UC Berkeley AMP 实验室,自开源以来经过 7 年的不断开发迭代,支撑大数据处理场景的数 ...
- 基于 Mesh 的统一路由在海外业务的实践
简介:本文主要介绍我们最近在利用 Service Mesh 架构解决海外业务问题中一些实践和价值探索.我们在海外业务引入 Mesh 架构过程中,充分利用 Istio 的基于 yaml 来描述和定义路 ...
- Java Map中那些巧妙的设计
简介: 他山之石可以攻玉,这些巧妙的设计思想非常有借鉴价值,可谓是最佳实践.然而,大多数有关Java Map原理的科普类文章都是专注于"点",并没有连成"线", ...
- 如何使用 Kubernetes 监测定位慢调用
简介:本次课程主要分为三大部分,首先将介绍慢调用的危害以及常见的原因:其次介绍慢调用的分析方法以及最佳实践:最后将通过几个案例来去演示一下慢调用的分析过程. 作者:李煌东 大家好,我是阿里云的李煌东 ...
- WPF 自定义控件入门 Focusable 与焦点
自定义控件时,如果自定义的控件需要用来接收键盘消息或者是输入法的输入内容,那就需要关注到控件的焦点 默认情况下的自定义控件是没有带可获取焦点的功能的,例如编写一个继承 FrameworkElement ...
- VisualStudio 如何快速添加一个 Git Tag 推送
在 VisualStudio 的团队管理功能,提供了方便的添加 Tag 的方法,可以新建一个 Tag 添加 Tag 信息,同时推送某个特定的 Tag 到服务器.配合推 Tag 打包 NuGet 的方法 ...
- css的animate做一个信号动画
html <div class="jump flex-fs fadeAndScaleIn"> <span></span> <span> ...