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 ...
随机推荐
- SqlServer2008发布订阅(数据同步)
目录 1. 发布必备条件 1.1. 数据库故障还原模型必需为完全还原模型 1.2. 数据库被同步的数据表必须有主键 1.3. 计算机名称来进行SQLServer服务器的注册 1.4. SQLServe ...
- Port forwarding with xinetd Ask
https://stackoverflow.com/questions/21716673/port-forwarding-with-xinetd --------------------------- ...
- Android Studio签名打包应用
转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50812391 可直接看看以下的Android Studio中签名应用 Android要 ...
- 编程算法 - n个骰子的点数(递归) 代码(C)
n个骰子的点数(递归) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把n个骰子仍在地上, 全部骰子朝上一面的点数之和为s. 输入n, 打印出 ...
- 创建一个Cordova完整应用
本文承接上篇<创建Cordova插件>,通过实现一个简单的应用作为这个Cordova0基础系列的结束. 前边对Cordova编程已经讲了不少了.还没有拿真实应用为例完整的演练一遍构建过程. ...
- go5--数组
package main /* 数组Array 定义数组的格式:var <varName> [n]<type>,n>=0 数组长度也是类型的一部分,因此具有不同长度的数组 ...
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...
- 22 WPF列表,树,网格
ListView ListView从ListBox派生,只增加了View属性.如果你没有设置View属性,ListView行为正如ListBox. 从技术上,View属性指向任何ViewBase派生类 ...
- redirect和forward 的区别
1.从地址栏显示来说 forward 是服务器请求资源,服务器直接访问目标地址url,把那个url的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容从哪里来的,所以他的地 ...
- 使用Google Closure Compiler全力压缩代码(转)
JavaScript压缩代码的重要性不言而喻,如今的压缩工具也有不少,例如YUI Compressor,Google Closure Compiler,以及现在比较红火的UglifyJS.Uglify ...