[leetcode] 690. Employee Importance

网友投稿 275 2022-08-27

[leetcode] 690. Employee Importance

Description

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

分析

题目的意思是:给定你一个数组,数组中的值依次代表雇员id,重要值,下级关系。现在要求返回给定雇员的总重要值(要求累加其雇员的重要值)。

一理解题意,马上就要想到是深度优先搜索。首先我们用hash表建立雇员与其下级的键值对,用集合s来标记遍历过的雇员。对遍历的每个雇员,累加其重要值和其下属的重要值。

代码

/*// Employee infoclass Employee {public: // It's the unique ID of each node. // unique id of this employee int id; // the importance value of this employee int importance; // the id of direct subordinates vector subordinates;};*/class Solution {public: int getImportance(vector employees, int id) { unordered_set s; unordered_map m; for(auto e:employees){ m[e->id]=e; } return solve(id,m,s); } int solve(int id,unordered_map& m,unordered_set& s){ if(s.count(id)) return 0; s.insert(id); int res=m[id]->importance; for(int num:m[id]->subordinates){ res+=solve(num,m,s); } return res; }};

参考文献

​​[LeetCode] Employee Importance 员工重要度​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 939. Minimum Area Rectangle
下一篇:[leetcode] 957. Prison Cells After N Days
相关文章

 发表评论

暂时没有评论,来抢沙发吧~