当前位置:网站首页>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


边栏推荐
- Summary of reptile knowledge points
- SDN_简单总结
- [ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- 樹結構---二叉樹2非遞歸遍曆
- 美团2022年机试
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- Log4j 日志框架
- Shell脚本-case in 和正则表达式
猜你喜欢

NiO zero copy

Redis——Lettuce连接redis集群

Implementation and application of queue

【pytorch】2.4 卷积函数 nn.conv2d

Understanding and implementation of AVL tree

Principles of Microcomputer - internal and external structure of microprocessor

Nacos service configuration and persistence configuration

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

【检测技术课案】简易数显电子秤的设计与制作

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
随机推荐
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
[interview brush 101] linked list
安装Oracle EE
[pytorch] 2.4 convolution function nn conv2d
nacos簡易實現負載均衡
Naoqi robot summary 28
laravel postman 提交表单出现419错误。2020年7月6日记。
Ape anthropology topic 20 (the topic will be updated from time to time)
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
Shell脚本-while循环详解
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
Implementation and application of queue
Is it safe to dig up money and make new shares
Tree structure --- binary tree 1
SDN_简单总结
Why is the Ltd independent station a Web3.0 website!
Can diffusion models be regarded as an autoencoder?
Tree structure -- binary tree 2 non recursive traversal
Niuke monthly race 22- collect pieces of paper
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database