LN : leetcode 690 Employee Importance
lc 690 Employee Importance
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
Note:
- One employee has at most one direct leader and may have several
subordinates. - The maximum number of employees won't exceed 2000.
BFS Accepted##
很经典的可以用BFS算法快速解决的问题。唯一需要注意的是push进队列的应该为id-1,而不是id,因为它是vector的下标。
/*
// 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) {
if (!employees.size()) return 0;
int total_value = 0;
queue<int> q;
q.push(id-1);
while (!q.empty()) {
auto employee = employees[q.front()];
q.pop();
total_value += employee->importance;
for (auto i : employee->subordinates) q.push(i-1);
}
return total_value;
}
};
LN : leetcode 690 Employee Importance的更多相关文章
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- 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——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...
随机推荐
- java 数据结构. 源代码阅读
Collections工具类里的 Collections.synchronizedList public static <T> List<T> synchronizedList ...
- Android 网络学习之获取server文本文件
上次我们学习怎样从网络上获取一张图片,今天我们学习怎样从网络上获取文本文件.以XML文件为样例. 由于XML文件在实际开发中最为常见. 我们以以下图片为样例学习怎样从网络上获取XML文件 我们的xml ...
- linux 经常使用网络命令
1. ifconfig ifconfig主要是能手动启动.观察和改动网络接口的相关參数.能改动的參数许多,包含IP參数及MTU等都能改动,他的语法例如以下: [root@linux ~]# ifco ...
- hdu 5361 2015多校联合训练赛#6 最短路
In Touch Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad 背包
C. Dima and Salad Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...
- Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列
D. Once Again... You a ...
- [RK3288][Android6.0] 音频调试方法小结【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/70053135 Platform: ROCKCHIPOS: Android 6.0Kernel ...
- RK3288的gpio设置【转】
本文转载自:http://blog.csdn.net/keleming1/article/details/51034766 转http://www.360doc.com/content/14/1227 ...
- missing required source folder
Eclipse 中XXX is missing required source folder 问题的解决 https://blog.csdn.net/itzhangdaopin/article/det ...
- luogu3899谈笑风生
https://www.zybuluo.com/ysner/note/1298140 题面 设\(T\)为一棵有根树,我们做如下的定义: 设\(a\)和\(b\)为\(T\)中的两个不同节点.如果\( ...