LeetCode 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.
题目分析及思路
定义了一个含有employee信息的数据结构,该结构包括了employee的唯一id、他的重要值以及他的直接下属的id。现给定一个公司的employee的信息,以及一个employee的id,要求返回这个employee以及他所有直接下属的重要值的总和。可以使用递归的方法,跳出递归的条件是当前employee没有直接下属,否则则遍历当前employee的所有下属获得重要值。
python代码
"""
# Employee info
class Employee:
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:
def getImportance(self, employees, id):
"""
:type employees: Employee
:type id: int
:rtype: int
"""
ids = [employee.id for employee in employees]
leader = employees[ids.index(id)]
employees.pop(ids.index(id))
if leader.subordinates == []:
return leader.importance
else:
im = 0
for id in leader.subordinates:
im += self.getImportance(employees, id)
return im + leader.importance
LeetCode 690 Employee Importance 解题报告的更多相关文章
- 【LeetCode】690. Employee Importance 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 日期 题目地址:https://le ...
- (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 ...
- 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 ...
- [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 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- Odoo 去掉 恼人的 "上午"和"下午"
- 删除最少字符生成Palindrome
看到一个哥们G家电面试题,求删除最少字符生成Palindrome.大神们都在讨论dp,我完全想不到. 放一个没优化的DFS. 乐神说是O(n3)复杂度, 有机会的话要减少使用substring,并且要 ...
- ref和引用类型传参的区别
引用类型对象本身不改变,只改变对象的属性时,我们在操作同一个对象:(类似c++指针指向的地址) 如果连对象本身都可能会改变,又要保证在操作同一个对象,就用ref传引用类型的对象吧!(类似c++指向一个 ...
- 从git上check out指定的文件夹至本地
当项目过大时,从服务器上拉取项目是件很头疼的事情,那么就说说怎么只拉区某个或几个文件夹至本地. git clone -n git@172.0.0.10:test/test_platform.git c ...
- CLOS网络
CLOS网络是指为了降低多级交换网络的成本,长期以来人们一直在寻找一种交叉点数随入.出现数增长较慢的交换网络,其基本思想都是采用多个较小规模的交换单元按照某种连接方式连接起来形成多级交换网络.
- hibernate 查询全部数据的三种方法
1.Query对象 使用Query对象需要写hql语句,使用hql语句操作的是实体类和属性. 用于查询全部的hql语句:from 实体类名称 例:String hql = "from U ...
- 《objective-c基础教程》学习笔记(四)—— OC面向对象编程初探
在上篇博文中,我们编写了一个可以输出不同几何类型的小程序.通过C语言的struct结构体,给大家感受了下,对象的大概样子. 如果用Obejctive-C的面向对象的特征来实现.那么,drawShape ...
- CentOS7 设置软件镜像源
Linux拥有众多的发行版,每个发行版都有提供镜像,但是,在国内这些镜像的下载速度参差不齐,偶尔会有更新失败的情况. 网上众多推荐的镜像源为163的镜像,笔者通过一段时间的试用,发现某些小众带宽访问1 ...
- 【GIS】Vue修改图层透明度
1.添加透明度控制条 <input id="slider" type="range" min="0" max="1" ...
- TensorFlow Jupyter Notebook 和matplotlib安装配置
Jupyter Notebook 和matplotlib Jupyter Notebook安装 Python 3 : python3 -m pip install --upgrade pip pyth ...