当前位置:网站首页>2022.02.15_ Daily question leetcode six hundred and ninety
2022.02.15_ Daily question leetcode six hundred and ninety
2022-07-01 09:13:00 【No い】
Title Description
690. The importance of employees
Given a data structure to store employee information , It includes employees Unique id , Importance and Direct subordinates id .
such as , staff 1 It's the employees 2 Leadership of , staff 2 It's the employees 3 Leadership of . Their corresponding importance is 15 , 10 , 5 . So employees 1 The data structure is [1, 15, [2]] , staff 2 Of The data structure is [2, 10, [3]] , staff 3 The data structure is [3, 5, []] . Note that although employees 3 It's also employees 1 One of my subordinates , But because of Not directly subordinate , So it's not reflected in the employees 1 In the data structure of .
Now enter all the employee information of a company , And individual employees id , Return the sum of the importance of this employee and all his subordinates .
Example :
Input :[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output :11
explain :
staff 1 The importance of oneself is 5 , He has two direct subordinates 2 and 3 , and 2 and 3 The importance of the two is 3 . So employees 1 The total importance of is 5 + 3 + 3 = 11 .
Tips :
An employee can have at most one Directly Leader , But there can be multiple Directly subordinate
No more than 2000 .
Ideas
Using recursion , Each time I traverse the set to find the corresponding id Equal employees ( have access to map For storage , It can reduce the time of traversing the array ), Accumulate their importance , And then on its Direct reports Continue accumulation again
【PS :】 You can also use breadth first search , Not yet ……( Later learning )
Code
class Solution {
public int getImportance(List<Employee> employees, int id) {
return f(employees, id, 0);
}
private int f(List<Employee> employees, int id, int res) {
for (Employee employee : employees) {
if (employee.id == id) {
int len = employee.subordinates.size();
res += employee.importance;
for (int i = 0; i < len; i++) {
res += f(employees, employee.subordinates.get(i),0);
}
break;
}
}
return res;
}
}
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap<>();
for (Employee e : employees) {
map.put(e.id,e);
}
return f(map, id, 0);
}
private int f(Map<Integer, Employee> map, int id, int res) {
Employee e = map.get(id);
res += e.importance;
for (int i = 0; i < e.subordinates.size(); i++) {
res += f(map, e.subordinates.get(i), 0);
}
return res;
}
}
Running results


边栏推荐
- Is it safe to dig up money and make new shares
- 樹結構---二叉樹2非遞歸遍曆
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
- 树结构---二叉树2非递归遍历
- SDN_ Simple summary
- Shell script -read command: read data entered from the keyboard
- Structure de l'arbre - - - arbre binaire 2 traversée non récursive
- Promise asynchronous programming
- 足球篮球体育比赛比分直播平台源码/app开发建设项目
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
猜你喜欢

OSPF - virtual link details (including configuration commands)

MySQL optimization

Imitation of Baidu search results top navigation bar effect

How to launch circle of friends marketing and wechat group activities

Learning practice: comprehensive application of cycle and branch structure (II)

Principles of Microcomputer - internal and external structure of microprocessor

Jeecg restart alarm 40001

nacos简易实现负载均衡

nacos服务配置和持久化配置

小鸟识别APP
随机推荐
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
3D printing Arduino four axis aircraft
猿人学第20题(题目会不定时更新)
2.4 激活函数
Shell script -read command: read data entered from the keyboard
【检测技术课案】简易数显电子秤的设计与制作
Shell脚本-数组定义以及获取数组元素
Shell脚本-read命令:读取从键盘输入的数据
The fixed assets management system enables enterprises to dynamically master assets
Shell脚本-字符串
【电赛训练】红外光通信装置 2013年电赛真题
通过 代码实例 理解 浅复制 与 深复制
Shell脚本-case in语句
It technology ebook collection
Design and manufacture of simple digital display electronic scale
How to solve the problem of fixed assets management and inventory?
PR training notes
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Shell脚本-case in 和正则表达式
树结构---二叉树2非递归遍历