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 ...
随机推荐
- Maven + spring + Mybatis + SpringMVC
tomcat版本:apache-tomcat-7.0.70 一.新建一个maven的web项目 1.1.请勾选“Create a simple project”,创建一个简单的项目,这里不使用模板.也 ...
- Luogu 3959 [NOIP2017] 宝藏- 状压dp
题解 真的想不到这题状压的做法...听说还有跑的飞快的模拟退火,要是现场做绝对滚粗QAQ. 不考虑深度,先预处理出 $pt_{i, S}$ 表示让一个不属于 集合 $S$ 的 点$i$ 与点集 $S$ ...
- Linux下timer延时的使用
http://blog.csdn.net/hzpeterchen/article/details/8090385 因笔者工作在嵌入式平台上(非x386),下面给出的结论仅在arm平台上测试过. 1. ...
- eclipse代码自动提示,eclipse设置代码自动提示
eclipse代码自动提示,eclipse设置代码自动提示 eclipse是很多JAVA开发者基本上都用的工具,用它可以很方便的开发JAVA代码,当编写JAVA代码时,大部分人都是按组合键[Alt+/ ...
- head first 设计模式文摘
1 欢迎来到设计模式世界:设计模式入门 2 让你的对象知悉现况:观察者模式 3 装饰对象:装饰者模式 4 工厂模式:烘烤OO的精华 5 单件模式:独一无二的对象 6 命令模式:封装调用 7 适配器模式 ...
- gj8 元类编程
8.1 property动态属性 from datetime import date, datetime class User: def __init__(self, name, birthday): ...
- 61 origin授控于MATLAB
官方教程:http://www.originlab.com/forum/topic.asp?TOPIC_ID=22339 学习自白东升老师originPRO8.0教程. 我用的是origin pro2 ...
- 简单的Java,Python,C,C++
Java 语言 //package main //注意不要添加包名称,否则会报错. import java.io.*; import java.util.*; cin.hasNext(); cin.h ...
- RepositionBars的用法和参数的意义(引用别人的)
MFC窗口位置管理详细分析及实例 在一般用MFC编写的程序的窗口客户区中,可能有好几个子窗口(具有WM_CHILD风格的窗口).上边是工具栏,中间是视图窗口,下边是状态栏.三个窗 口在框架的客户区里和 ...
- OpenGL中位图的操作(glReadPixels,glDrawPixels等)
OpenGL中位图的操作 OpenGL中位图的操作(glReadPixels,glDrawPixels和glCopyPixels应用举例). 1. BMP文件格式简单介绍 BMP文件是一种像素文件,它 ...