(BFS) leetcode 690. Employee Importance
377369FavoriteShare
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, []]], 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.
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,不过,关于C++的类与指针的用法一度难倒了我。。。。
C++代码:
/*
// Employee info
class 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<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int res = ;
queue<int> q{{id}};
unordered_map<int,Employee*> m;
for(auto e:employees) m[e->id] = e;
while(!q.empty()){
auto t = q.front();
q.pop();
res += m[t]->importance;
for(int num:m[t]->subordinates){
q.push(num);
}
}
return res;
}
};
(BFS) leetcode 690. Employee Importance的更多相关文章
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690. Employee Importance (职员的重要值)
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode - 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- [LeetCode]690. Employee Importance员工重要信息
哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- [LeetCode] 690. Employee Importance_Easy tag: BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
随机推荐
- k8s(一) kubeadm简单集群初始化
写给想入门kubernetes的同学们 # 系统版本 [root@master ~]# cat /etc/os-release NAME="CentOS Linux" VERSIO ...
- Reading Text from Images Using C#
Introduction By using Optical Character Recognition (OCR), you can detect and extract handwritten an ...
- codevs2822
解题思路: tarjan缩点后算出度为0的点有几个,如果只有一个且这个点为爱心天使就行了: #include<iostream> #include<algorithm> #in ...
- servlet篇 之 访问形式
get方式访问和post方式访问: get/post区别? 1) 参数传递 查询字符串(的形式)! get url?key1=value&key2=value 2) http协议 请求报文包 ...
- js 异步代码
这段时间一直在用node.js做毕设的后台,所以需要一些异步代码操作,主要的异步方式有:Promise.Generator 和 async / await,但下面主要讲 Promise 和 async ...
- 【数学建模】day11-典型相关分析
这与主成分分析有点相似. 0. 基本思想主成分分析(PCA)是把原始有相关性变量,线性组合出无关的变量(投影),以利用主成分变量进行更加有效的分析.而典型相关分析(CCA)的思想是: 分析自变量组 X ...
- fastjson的JSONArray和JSONObject
转自: http://blog.csdn.net/tangerr/article/details/76217924 Fastjson是国内著名的电子商务互联网公司阿里巴巴内部开发的用于java后台处理 ...
- 常用的redis服务命令。
卸载服务:redis-server --service-uninstall 开启服务:redis-server --service-start 停止服务:redis-server --service- ...
- BZOJ 1977 严格次小生成树(算竞进阶习题)
树上倍增+kruskal 要找严格次小生成树,肯定先要找到最小生成树. 我们先把最小生成树的边找出来建树,然后依次枚举非树边,容易想到一种方式: 对于每条非树边(u,v),他会与树上的两个点构成环,我 ...
- MT【283】图像有唯一公共点.
函数$f(x)=\sqrt[n]x(n-\ln x),$其中$n\in N^*,x\in(0,+\infty)$.(1)若$n$为定值,求$f(x)$的最大值.(2)求证:对任意$m\in N^+$, ...