当前位置:网站首页>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
边栏推荐
- JCL 和 SLF4J
- nacos简易实现负载均衡
- Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
- Design and manufacture of simple digital display electronic scale
- Shell script case in and regular expressions
- Preparing for the Blue Bridge Cup -- bit operation
- Shell script - array definition and getting array elements
- SDN_简单总结
- Bird recognition app
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
猜你喜欢
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Pain points and solutions of fixed assets management of group companies
钓鱼识别app
小鸟识别APP
Nacos service configuration and persistence configuration
3D printing Arduino four axis aircraft
Which method is good for the management of fixed assets of small and medium-sized enterprises?
2.4 activation function
Simple load balancing with Nacos
NiO zero copy
随机推荐
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Flink面试题
Serialization, listening, custom annotation
Flink interview questions
通过 代码实例 理解 浅复制 与 深复制
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
NoSQL数据库的安装和使用
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
Installation and use of NoSQL database
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
2.2 【pytorch】torchvision. transforms
Ape anthropology topic 20 (the topic will be updated from time to time)
Pain points and solutions of fixed assets management of group companies
Principles of Microcomputer - internal and external structure of microprocessor
Shell script echo command escape character
How to solve the problem of fixed assets management and inventory?
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
美团2022年机试
Set the type of the input tag to number, and remove the up and down arrows