Breadth-first Search-690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his importance value and his directsubordinates' 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.
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int,Employee*> map ;
for (auto e : employees){
map[e->id] = e ;
} return dfs(map , id) ;
} int dfs(unordered_map<int,Employee*> map , int id){
int sum = map[id]->importance ;
for (auto sub_id : map[id]->subordinates){
sum += dfs(map,sub_id) ;
}
return sum ;
}
};
Breadth-first Search-690. Employee Importance的更多相关文章
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- 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&Python] Problem 690. Employee Importance
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 690. Employee Importance
好几种写法,这里贴几个出来 第一种:暴力解法,除去递归栈,空间复杂度O(1).时间复杂度略高 /* // Employee info class Employee { public: // It's ...
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
随机推荐
- 如何移除 input type="number" 时浏览器自带的上下箭头?
Chrome 下 input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: no ...
- bluez蓝牙测试工具
http://blog.csdn.net/talkxin/article/details/50610984
- 7.25 js 自定义方法 a.b
调用: $(function(){ Starinput.initiate({name:'qr1_z.startlevel',tar: $("#sitelogo1"), stars: ...
- hdu-1107(模拟题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1107 注意:1.路线是要反向的,走不通就反向: 2.输入输出全部是整形 3.攻击力不断变化 #incl ...
- POI解析Excel文件
@Test public void test1() throws FileNotFoundException, IOException{ HSSFWorkbook workbook = new HSS ...
- 获得硬盘的ID序列号(XE10.1+WIN8.1)
疯狂delphi DelphiXE公开课群:100162924.58593121 朱建强QQ:513187410 获得硬盘的ID序列号(XE10.1+WIN8.1) 相关资料: https://zhi ...
- Android-Java卖票案例-推荐此方式Runnable
上一篇博客 Android-卖票案例static-不推荐此方式,讲解了卖票案例是 private static int ticket = 10;,static静态的这种方式来解决卖票多卖30张的问题, ...
- 【WinRT】使用 T4 模板简化字符串的本地化
在 WinRT 中,对控件.甚至图片资源的本地化都是极其方便的,之前我在博客中也介绍过如何本地化应用名称:http://www.cnblogs.com/h82258652/p/4292157.html ...
- Jenkins中执行docker命令报错
Cannot connect to the Docker daemon. Is the docker daemon running on this host? 在配置Jenkins从Gitlab自 ...
- 在 IIS8 中保持网站持续运行
在早期版本的 IIS 中执行轮询任务不那么可靠.应用程序池回收后,网站不会自动重启,在新的请求激活应用程序之前,轮询任务不起作用.为了解决这个问题,需要引入外力驱动 Web 端执行任务,如图: 此方式 ...