leetcode-690. Employee Importance
来源:互联网 发布:区块链技术 数据存储 编辑:程序博客网 时间:2024/06/06 13:14
690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his importance value and his directsubordinates' 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, []]], 1Output: 11Explanation: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.
解法:就是类似树的遍历,BFS和DFS都可以写。
/*// Employee infoclass 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) { int sum =0; Stack <Integer> stack = new Stack<>(); Map<Integer, Employee> data = new HashMap<>(); for (int i = 0; i < employees.size(); i++) { if(employees.get(i).id==id){ stack.addAll(employees.get(i).subordinates); sum+=employees.get(i).importance; } //sum+=employees.get(i).importance; data.put(employees.get(i).id, employees.get(i));//用map查找应该会更快吧 } while(!stack.empty()){ int cur = stack.pop(); sum+=data.get(cur).importance; stack.addAll(data.get(cur).subordinates);//查找对应的employee } return sum; }}
- [LeetCode] 690. Employee Importance
- leetcode 690. Employee Importance
- [LeetCode] 690. Employee Importance
- leetcode-690. Employee Importance
- LeetCode 690.Employee Importance
- Leetcode:690. Employee Importance
- 690 leetcode employee importance
- LeetCode # 690 Employee Importance
- LeetCode.690 Employee Importance
- LeetCode-- Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- DFS:690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- jdk版本调整
- Java数据结构----图的基础知识
- 辉光管电子时钟DIY记录-静态扫描驱动方式
- 关于API与SDK的一些小知识
- 自定义单点登录项目 (1)
- leetcode-690. Employee Importance
- 探索多元化的知识付费工具
- Linux中文件与目录的传输、归档以及压缩
- spring中@Transactional注解抛出异常不回滚的现象
- Vim中的全局替换
- typedef函数指针的用法
- 从CNN视角看在自然语言处理上的应用
- linux上安装mysql数据库
- maven父工程问题dependencymanagement 和dependencies区别