(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;
}
};
最新文章
- URL安全的Base64编码
- delphi 中TStringList Clear 方法的时候该对象有没有被释放
- 为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换
- ZOJ Problem Set - 3635
- (转)GDT与LDT
- 读取sd卡下图片,由图片路径转换为bitmap
- web 基础设置
- 1.03-get_params2
- js-canvas(基本用法)
- C# pdf转word
- 树莓派raspberry pi配置无线路由器AP
- linux基础之系统管理类命令
- [转载]Best Practices for Speeding Up Your Web Site
- certbot自动在ubuntu16.04的nginx上部署let's encrypt免费ssl证书
- How to set up github to work with Visual Studio 2013
- python-原型模式
- jquery 中 $ 符的意义
- 数据仓库基础(十一)Informatica小技巧(2)
- 【刷题】BZOJ 2588 Spoj 10628. Count on a tree
- 在Linux上使用C语言编程获取IPv4地址及子网掩码
热门文章
- TestNG之测试执行后没有生成默认测试报告(IDEA)
- Vuex的API文档
- Zero to Build: Create new Xamarin apps in minutes with AppMap
- Build 2017 Revisited: .NET, XAML, Visual Studio
- Codeforces#543 div2 B. Mike and Children(暴力?)
- 只要访问url地址 那么容器就会根据地址进行对象的创建
- name设置id的方式 解决多个单选域冲突现象 同时有利于从动态网页取值
- [洛谷P1272] 重建道路
- 2019西北工业大学程序设计创新实践基地春季选拔赛 I Chino with Rewrite (并查集+树链剖分+线段树)
- MT【288】必要性探路