博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Employee Importance 员工重要度
阅读量:7114 次
发布时间:2019-06-28

本文共 2798 字,大约阅读时间需要 9 分钟。

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:

    1. One employee has at most one direct leader and may have several subordinates.
    2. The maximum number of employees won't exceed 2000.

这道题定义了一种员工类,有id,重要度,和direct report的员工,让我们求某个员工的总重要度。我们要明白的是就算某个员工不直接向你汇报工作,而是向你手下人汇报,这个人的重要度也会算进你的重要度中。这其实就是之前那道的变化形式,我们可以用DFS来做。首先我们想,为了快速的通过id来定位到员工类,需要建立一个id和员工类的映射,然后我们根据给定的员工id来算其重要度。计算方法当然是其本身的重要度加上其所有手下人的重要度,对于手下人,还要累加其手下人的重要度,为了不重复计算某个员工的重要度,我们建立一个集合,将遍历过的员工id放到集合中,这样一旦我们遍历到集合中有的员工,直接返回0即可;否则就将该员工id加入集合中,然后建立一个结果res变量,加上当前员工的重要度,然后遍历其所有手下,对其每个手下人调用递归函数加到res上,最后返回res即可,参见代码如下:

解法一:

public:    int getImportance(vector
employees, int id) { unordered_set
s; unordered_map
m; for (auto e : employees) m[e->id] = e; return helper(id, m, s); } int helper(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 += helper(num, m, s); } return res; }};

我们也可以用BFS来做,使用一个queue来辅助运算,开始将给定员工id放入,然后当queue不为空进行循环,每次取出队首员工,如果已经访问过了,直接跳过,否则加入集合中,然后累加上当前员工的复杂度到结果res,然后将其所有手下人加入队列等待遍历,参见代码如下:

解法二:

public:    int getImportance(vector
employees, int id) { int res = 0; queue
q{
{id}}; unordered_set
s; unordered_map
m; for (auto e : employees) m[e->id] = e; while (!q.empty()) { auto t = q.front(); q.pop(); if (s.count(t)) continue; s.insert(t); res += m[t]->importance; for (int num : m[t]->subordinates) { q.push(num); } } return res; }};

本文转自博客园的博客,原文链接:

,如需转载请自行联系原博主。

你可能感兴趣的文章
不变模式-类行为型
查看>>
正则表达式学习笔记
查看>>
LVM2逻辑卷之1——创建及扩容
查看>>
grub.conf加密
查看>>
WSFC时间分区场景实作
查看>>
The Receiver 4.4 - 客户端硬件解码 - 大幅度提升3D显示效能
查看>>
解决 yum时 Error: Protected multilib versions报错
查看>>
前端基础---HTML
查看>>
线程池
查看>>
理解RESTful架构
查看>>
windows_learn 002 用户管理和组策略
查看>>
linux中的邮件服务器笔记
查看>>
linux命令:w、who、whoami、last、lastb、lastlog、basename、mail、hostname
查看>>
Python---函数---默认参数
查看>>
collections.Counter. most_common
查看>>
【C#】让ListBox控件支持双击事件
查看>>
mysql常用备份还原命令
查看>>
交换机IOS失效的恢复详解
查看>>
awk支持多个记录分隔符的写法
查看>>
OCI之Docker标准浅谈
查看>>