lc 690 Employee Importance


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:

  1. One employee has at most one direct leader and may have several

    subordinates.
  2. 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的更多相关文章

  1. (BFS) leetcode 690. Employee Importance

    690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...

  2. LeetCode 690. Employee Importance (职员的重要值)

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  3. LeetCode - 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  4. LeetCode 690 Employee Importance 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  5. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  6. [LeetCode]690. Employee Importance员工重要信息

    哈希表存id和员工数据结构 递归获取信息 public int getImportance(List<Employee> employees, int id) { Map<Integ ...

  7. 690. Employee Importance - LeetCode

    Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...

  8. 【Leetcode_easy】690. Employee Importance

    problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...

  9. 【LeetCode】690. Employee Importance 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...

随机推荐

  1. Java并发包——线程通信

    Java并发包——线程通信 摘要:本文主要学习了Java并发包里有关线程通信的一些知识. 部分内容来自以下博客: https://www.cnblogs.com/skywang12345/p/3496 ...

  2. springmvc json 数据

    这里是controllor层 @RequestMapping("/traceupdatestatus") @ResponseBody public boolean traceupd ...

  3. Ubuntu 16.04通过Magent搭建Memcached集群(转)

    一.下载Magent 官网:https://code.google.com/archive/p/memagent/downloads 离线版本:(链接: https://pan.baidu.com/s ...

  4. JS原生DOM操作总结

    DOM的主要操作——增.删.改.查节点 (1) 查找节点 document.getElementById('div1') document.getElementsByName('uname') doc ...

  5. C# VS如何整个项目中查找字符串

    Ctrl+F打开查找对话框,然后输入查找字符串,电机右边的小三角,选择整个解决方案,就可以遍历所有文件查找指定字符了                                

  6. uva 439 Knight Moves 骑士移动

    这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...

  7. Android--Activity在跳转时携带数据

    首先看看两种传递方法演示样例:(一个简单姻缘计算器) 主Activity import android.os.Bundle; import android.app.Activity; import a ...

  8. 【动态规划】Dynamic Programming

    动态规划 一.动态规划 动态规划(Dynamic Programming)是一种设计的技巧,是解决多阶段决策过程最优化问题的通用方法. 基本思想:将待求解问题分解成若干个子问题,先求解子问题,然后从这 ...

  9. 经典的printk 写法

    经典的printk 写法: printk("[lynn--%s@%d]: addr:0x%x  \n",__func__,__LINE__,obj->client->a ...

  10. POJ 2260:Error Correction

    Error Correction Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6014   Accepted: 3886 ...