[LeetCode&Python] Problem 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
- 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:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won't exceed 2000.
- """
- # 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
- """
- emap={e.id:e for e in employees}
- def dfs(sid):
- em=emap[sid]
- return em.importance+sum(dfs(sid) for sid in em.subordinates)
- return dfs(id)
[LeetCode&Python] Problem 690. Employee Importance的更多相关文章
- 【Leetcode_easy】690. Employee Importance
problem 690. Employee Importance 题意:所有下属和自己的重要度之和,所有下属包括下属的下属即直接下属和间接下属. solution:DFS; /* // Employe ...
- (BFS) leetcode 690. Employee Importance
690. Employee Importance Easy 377369FavoriteShare You are given a data structure of employee informa ...
- LN : leetcode 690 Employee Importance
lc 690 Employee Importance 690 Employee Importance You are given a data structure of employee inform ...
- 690. Employee Importance - LeetCode
Question 690. Employee Importance Example 1: Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1 Outp ...
- 【LeetCode】690. Employee Importance 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...
- LeetCode 690 Employee Importance 解题报告
题目要求 You are given a data structure of employee information, which includes the employee's unique id ...
- 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——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
随机推荐
- nginx 拒绝本地ip访问
server { listen default_server; server_name _; server_name localhost; ; }
- Android Studio向项目添加C/C++原生代码教程
说明:本文相当于官方文档的个人重新实现,官方文档链接:https://developer.android.com/studio/projects/add-native-code 向项目添加C/C++代 ...
- Mybatis排序无效问题解决
在 mybatis 的 xml中,为一个SQL语句配置order by 子句时,需要这个排序的字段是前端传递过来的,而且排序的顺序(升序 OR 降序)也是由前端传递过来的.对于这种需求,我起初写成了下 ...
- Oracle中使用PL/SQL如何定义参数、参数赋值、输出参数和 if 判断
1.pl/sql如何定义参数 declare --1)定义参数 -- ban_Id number; ban_Name ); 2.pl/sql如何参数赋值 --2)参数赋值-- ban_Id :; ba ...
- 神奇的口袋(dp)
有一个神奇的口袋,总的容积是40,用这个口袋可以变出一 些物品,这些物品的总体积必须是40. John现在有n(1≤n ≤ 20)个想要得到的物品,每个物品 的体积分别是a1,a2……an.John可 ...
- 2-MAVEN 基本命令
MVN的基本命令 mvn package:打包 >生成了target目录 >编译了代码 >使用junit测试并生成报告 >生成代码的jar文件 >运行jar包: java ...
- Unity中物体碰撞后去掉相互之间的反弹力
最近自制了一个的角色控制器(没有重力的角色)时发现,角色碰撞到墙壁之后会有一个小小的反弹力导致角色有一个微弱的反弹位移,这样给人一种不好的感觉.研究了一下,除了限制坐标轴( Rigidbody---C ...
- xshell提示必须安装最新的更新
今天大家的xshell基本都出了这个问题 调整时间,调整到比较前的时间,打开xshell即可. 然后工具->选项 把更新去了
- vue-3-Class 与 Style 绑定
对象语法: <div v-bind:class="{ active: isActive }"></div> <div class="stat ...
- 十三. Python基础(13)--生成器进阶
十三. Python基础(13)--生成器进阶 1 ● send()方法 generator.send(value) Resumes the execution, and "sends&qu ...