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 ...
随机推荐
- codevs——1013 求先序排列
1013 求先序排列 2001年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 给出 ...
- MySQL JDBC URL参数(转)
MySQL的 JDBC URL格式: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1][=prope ...
- UIButton图片文字位置的四种情况
我们在做项目的过程中经常会遇到各定制UIButton 1.左边图片,右边文字 2.左边文字,右边图片 3.上边图片,下边文字 4.上边文字,下边图片 针对这四种情况 使用UIButton的catego ...
- 基于Wi-Fi的HID注射器,利用WHID攻击实验
WHID代表基于 Wi-Fi 的 HID 注射器,即对 HID 攻击进行无线化攻击的一种注入工具. 实验攻击原理如下图: 攻击者使用ESP8266作为AP,在自己的电脑创建客户端连接AP.在客户端键入 ...
- 条款十六: 在operator=中对所有数据成员赋值
当涉及到继承时,派生类的赋值运算符也必须处理它的基类成员的赋值!否则,当派生类对象向另一个派生类对象赋值时,只有派生类部分赋值了.看看下面: class base { public: ): x(ini ...
- android意图传參数(四)
一.依照向导创建一个project,layout的activity_main.xml文件内容例如以下: <RelativeLayout xmlns:android="http://sc ...
- hdu 3006 The Number of set(思维+壮压DP)
The Number of set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Codeforces Round #100 A. New Year Table
A. New Year Table time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- BZOJ_2424_[HAOI2010]订货_最小费用最大流
BZOJ_2424_[HAOI2010]订货_最小费用最大流 Description 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付 ...
- Windows路径
绝对路径 是从盘符开始的路径,形如 C:\windows\system32\cmd.exe 相对路径 是从当前目录开始的路径,假如当前目录为C:\windows 要描述上述路径,只需输入 system ...