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
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.

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:tree的层序遍历思想

"""
# Employee info
class Employee(object):
def __init__(self, id, importance, subordinates):
# It's the unique id of each node.
# unique id of this employee
self.id = id
# the importance value of this employee
self.importance = importance
# the id of direct subordinates
self.subordinates = subordinates
"""
class Solution(object):
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
"""
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
use dict to find its sub employee
"""
emp_dict = {e.id:e for i,e in enumerate(employees)}
root = emp_dict[id]
# tree BFS
q = [root]
ans = 0
while q:
q2 = []
for node in q:
ans += node.importance
for i in node.subordinates:
q2.append(emp_dict[i])
q = q2
return ans

DFS:

"""
# Employee info
class Employee(object):
def __init__(self, id, importance, subordinates):
# It's the unique id of each node.
# unique id of this employee
self.id = id
# the importance value of this employee
self.importance = importance
# the id of direct subordinates
self.subordinates = subordinates
"""
class Solution(object):
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
"""
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
use dict to find its sub employee
"""
emp_dict = {e.id:e for i,e in enumerate(employees)}
root = emp_dict[id] # tree DFS
def dfs(root, emp_dict):
score = root.importance
for i in root.subordinates:
score += dfs(emp_dict[i], emp_dict)
return score return dfs(root, emp_dict)

leetcode 690. Employee Importance——本质上就是tree的DFS和BFS的更多相关文章

  1. (BFS) leetcode 690. Employee Importance

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

  2. LN : leetcode 690 Employee Importance

    lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...

  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, his ...

  5. LeetCode 690 Employee Importance 解题报告

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

  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_Easy tag: BFS

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

随机推荐

  1. 远程连接mysql数据库

    远程连接mysql数据库需要以下步骤(改表法): 1.数据源(数据库服务器)对要访问数据库进行授权: 2.本地库(本地)连接: 具体方法: 1. 进入数据服务器,输入cmd进入控制台:输入 mysql ...

  2. Ztree节点前加上两个自定义按钮

    前言: 在我的权限管理模块遇到了给某些角色加权限的问题,这时就需要实现将每个模块做成树,在每个节点前加上预览和编辑的按钮,这样可以根据数据库的某个字段给每个角色赋权限. 必须必须吐槽的是,这部分的功能 ...

  3. 零基础入门学习Python(21)--函数:lambda表达式

    知识点 lambda 表达式 Python 允许使用lambda关键字创建匿名函数 lambda 函数怎么使用? 单个参数 >>> def add(x): return 2*x + ...

  4. Django之ORM操作(***)

    Django之ORM操作(***) http请求的流程: url--> 视图view(模板+数据库) --> ORM的功能: 可以转换SQL语句,并做操作. ORM操作数据表: -创建表: ...

  5. Python爬虫例子(笔记,不适合参考,愿意看的可以看看)

    话不多说,直接上代码: import re import csv #爬虫的一个小例子,爬的是百度贴吧(网页版)某个帖子的各个楼层的用户名,发言内容和发言时间(使用到了正则表达式) source3.tx ...

  6. codeforces 363A

    #include<stdio.h>//这题挺有意思小学学的算盘 int main() { int n,i,m; while(scanf("%d",&n)!=EO ...

  7. 【BZOJ4868】期末考试(整数三分)

    题意: 有n位同学,每位同学都参加了全部的m门课程的期末考试,都在焦急的等待成绩的公布.第i位同学希望在第ti天 或之前得知所.有.课程的成绩.如果在第ti天,有至少一门课程的成绩没有公布,他就会等待 ...

  8. Codeforces704B. Ant Man

    n<=5000个数轴上的点,有属性x,a,b,c,d,从i跳到j的代价如下: 问从s跳到t的最小代价. 方法?:先构造s->t链,然后依次插入其他点,每次选个最佳的位置.过了这题,正确性不 ...

  9. msp430入门编程00

    msp430单片机最小系统 msp430入门学习 msp430入门编程

  10. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...