LeetCode - 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, []]], 1
Output: 11
Explanation:
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.
/*
// Employee info
class 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) {
        return get(employees, id);
    }

    private int get(List<Employee> employees, int id) {
        int total = 0;
        for (Employee e : employees) {
            if (id == e.id) {
                total = e.importance;
                for (int subId : e.subordinates) {
                    total += get(employees, subId);
                }
                break;
            }
        }
        return total;
    }
}

原文地址:https://www.cnblogs.com/wxisme/p/8312704.html

时间: 2024-10-09 05:36:16

LeetCode - 690. Employee Importance的相关文章

[LeetCode]690. Employee Importance员工重要信息

哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integer,Employee> map = new HashMap<>(); for (int i = 0; i < employees.size(); i++) { Employee temp = employees.get(i); map.put(temp.id,temp); } ret

[LeetCode&amp;Python] Problem 690. Employee Importance

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. Th

【Breadth-first Search】690. Employee Importance(easy)

#week4# #from leetcode# Description 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 employe

690. Employee Importance 员工重要性

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. Th

[LeetCode] 690. Employee Importance_Easy tag: BFS

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. The

leetcode 690.员工的重要性

题目: 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结构是[1, 15, [2]],员工2的数据结构是[2, 10, [3]],员工3的数据结构是[3, 5, []].注意虽然员工3也是员工1的一个下属,但是由于并不是直系下属,因此没有体现在员工1的数据结构中. 现在输入一个公司的所有员工信息,以及单个员工id,返回这个员工和他所有下属的重要度之和.

[LeetCode]577. Employee Bonus 员工奖金

Select all employee's name and bonus whose bonus is < 1000. Table:Employee +-------+--------+-----------+--------+ | empId | name | supervisor| salary | +-------+--------+-----------+--------+ | 1 | John | 3 | 1000 | | 2 | Dan | 3 | 2000 | | 3 | Brad

Leetcode 759. Employee Free Time

思路:区域覆盖问题.一个自然的想法是将每个员工的工作时间段看做一个木棒,每个木棒的长度就是这个时间段的时长.然后按照木棒的起始位置升序排列,接着由低位置向高位置一个木棒一个木棒的看过去.如果当前木棒的末节点的位置>下一个木棒的头节点位置,那么这两个节点就是一个free time的开头和结尾:如果当前木棒的末节点位置<=下一个木棒的头节点位置,那么更新当前木棒的末节点位置为max(当前木棒的末节点位置,下一个木棒的头节点位置). 1 /** 2 * Definition for an inter

算法与数据结构基础 - 广度优先搜索(BFS)

BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi