列表

详情


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 ,返回这个员工和他所有下属的重要度之和。

 

示例:

输入:[[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
输出:11
解释:
员工 1 自身的重要度是 5 ,他有两个直系下属 2 和 3 ,而且 2 和 3 的重要度均为 3 。因此员工 1 的总重要度是 5 + 3 + 3 = 11 。

 

提示:

相似题目

嵌套列表权重和

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/* // Definition for Employee. class Employee { public: int id; int importance; vector<int> subordinates; }; */ class Solution { public: int getImportance(vector<Employee*> employees, int id) { } };

java 解法, 执行用时: 5 ms, 内存消耗: 45.1 MB, 提交时间: 2024-08-26 09:40:22

/*
// Definition for Employee.
class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
};
*/

class Solution {
    public int getImportance(List<Employee> employees, int id) {
        Map<Integer, Employee> employeeMap = new HashMap<>(employees.size());
        for (Employee e : employees) {
            employeeMap.put(e.id, e);
        }
        return dfs(employeeMap, id);
    }

    private int dfs(Map<Integer, Employee> employeeMap, int id) {
        Employee e = employeeMap.get(id);
        int res = e.importance;
        for (int subId : e.subordinates) {
            res += dfs(employeeMap, subId);
        }
        return res;
    }
}

javascript 解法, 执行用时: 60 ms, 内存消耗: 53.2 MB, 提交时间: 2024-08-26 09:40:03

/**
 * Definition for Employee.
 * function Employee(id, importance, subordinates) {
 *     this.id = id;
 *     this.importance = importance;
 *     this.subordinates = subordinates;
 * }
 */

/**
 * @param {Employee[]} employees
 * @param {number} id
 * @return {number}
 */
var GetImportance = function(employees, id) {
    const employeeMap = new Map();
    for (const e of employees) {
        employeeMap.set(e.id, e)
    }
    function dfs(id) {
        const e = employeeMap.get(id);
        let res = e.importance;
        for (const subId of e.subordinates) {
            res += dfs(subId);
        }
        return res;
    }
    return dfs(id);
};

cpp 解法, 执行用时: 28 ms, 内存消耗: 19.8 MB, 提交时间: 2024-08-26 09:39:20

/*
// Definition for Employee.
class Employee {
public:
    int id;
    int importance;
    vector<int> subordinates;
};
*/

class Solution {
public:
    int getImportance(vector<Employee*>& employees, int id) {
        unordered_map<int, Employee*> employee_map;
        for (auto e : employees) {
            employee_map[e->id] = e;
        }
        auto dfs = [&](auto&& dfs, int id) -> int {
            auto e = employee_map[id];
            int res = e->importance;
            for (int sub_id : e->subordinates) {
                res += dfs(dfs, sub_id);
            }
            return res;
        };
        return dfs(dfs, id);
    }
};

python3 解法, 执行用时: 126 ms, 内存消耗: 17.7 MB, 提交时间: 2024-08-26 09:38:44

"""
# Definition for Employee.
class Employee:
    def __init__(self, id: int, importance: int, subordinates: List[int]):
        self.id = id
        self.importance = importance
        self.subordinates = subordinates
"""

class Solution:
    def getImportance(self, employees: List['Employee'], id: int) -> int:
        employees = {e.id: e for e in employees}
        def dfs(id: int) -> int:
            e = employees[id]
            return e.importance + sum(dfs(sub) for sub in e.subordinates)
        return dfs(id)

python3 解法, 执行用时: 132 ms, 内存消耗: 17.7 MB, 提交时间: 2024-08-26 09:35:04

"""
# Definition for Employee.
class Employee:
    def __init__(self, id: int, importance: int, subordinates: List[int]):
        self.id = id
        self.importance = importance
        self.subordinates = subordinates
"""

class Solution:
    def getImportance(self, employees: List['Employee'], id: int) -> int:
        mp = dict()
        total = 0
        for e in employees:
            mp[e.id] = e
    
        queue = [id]
        while len(queue) > 0:
            e = mp[queue[0]]
            total += e.importance
            queue = queue[1:] + e.subordinates
        return total

golang 解法, 执行用时: 12 ms, 内存消耗: 6.7 MB, 提交时间: 2021-06-02 10:23:52

/**
 * Definition for Employee.
 * type Employee struct {
 *     Id int
 *     Importance int
 *     Subordinates []int
 * }
 */

func getImportance(employees []*Employee, id int) (total int) {
	mp := map[int]*Employee{}
	for _, employee := range employees {
		mp[employee.Id] = employee
	}
	// dfs遍历
	var dfs func(id int)
	dfs = func(id int) {
		employee := mp[id]
		total += employee.Importance
		for _, subId := range employee.Subordinates {
			dfs(subId)
		}
	}
	dfs(id)

	return total
}

golang 解法, 执行用时: 12 ms, 内存消耗: 6.7 MB, 提交时间: 2021-06-02 10:23:20

/**
 * Definition for Employee.
 * type Employee struct {
 *     Id int
 *     Importance int
 *     Subordinates []int
 * }
 */

func getImportance(employees []*Employee, id int) (total int) {
	mp := map[int]*Employee{}
	for _, employee := range employees {
		mp[employee.Id] = employee
	}
	// bfs遍历
	queue := []int{id}
	for len(queue) > 0 {
		employee := mp[queue[0]]
		total += employee.Importance
		queue = append(queue[1:], employee.Subordinates...)
	}

	return total
}

上一题