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


边栏推荐
- Tree structure --- binary tree 1
- Shell脚本-变量的定义、赋值和删除
- 钓鱼识别app
- jeecg 重启报40001
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- Is it safe to dig up money and make new shares
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- [pytorch] softmax function
- Serialization, listening, custom annotation
- Latex插入的eps图片模糊解决方法
猜你喜欢

3D打印Arduino 四轴飞行器
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

Imitation of Baidu search results top navigation bar effect

nacos简易实现负载均衡

Which method is good for the management of fixed assets of small and medium-sized enterprises?

Installation and use of NoSQL database

Ape anthropology topic 20 (the topic will be updated from time to time)

MySQL optimization

How to launch circle of friends marketing and wechat group activities

Football and basketball game score live broadcast platform source code /app development and construction project
随机推荐
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
足球篮球体育比赛比分直播平台源码/app开发建设项目
Shell脚本-变量的定义、赋值和删除
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
2.4 activation function
小鸟识别APP
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
樹結構---二叉樹2非遞歸遍曆
Jeecg restart alarm 40001
SDN_ Simple summary
Simple load balancing with Nacos
SDN_简单总结
Shell script case in statement
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Shell script -for loop and for int loop
Why is the Ltd independent station a Web3.0 website!
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Principle and application of single chip microcomputer timer, serial communication and interrupt system
【pytorch学习】torch.device
jeecg 重启报40001