问题描述:简单型,BFS解法

You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his directsubordinates’ 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.

40/108

"""
# Employee info
class Employee:def __init__(self, id, importance, subordinates):# It's the unique id of each node.# unique id of this employeeself.id = id# the importance value of this employeeself.importance = importance# the id of direct subordinatesself.subordinates = subordinates
"""
class Solution:def getImportance(self, employees, id):""":type employees: Employee # 是个列表,列表中的数据类型是Employee:type id: int:rtype: int"""weight_sum = 0ids = [id] # 用于存储相关的idwhile employees != []:employee = employees.pop(0) # 排在第一的不一定是最高领导,乱序[cur_id, weight, sub_lst] = employee.id,employee.importance, employee.subordinates if cur_id in ids:weight_sum += weightids += sub_lstreturn weight_sum

93/108

"""
# Employee info
class Employee:def __init__(self, id, importance, subordinates):# It's the unique id of each node.# unique id of this employeeself.id = id# the importance value of this employeeself.importance = importance# the id of direct subordinatesself.subordinates = subordinates
"""
class Solution:def getImportance(self, employees, id):""":type employees: Employee # 是个列表,列表中的数据类型是Employee:type id: int:rtype: int"""weight_sum = 0ids = [id] # 用于存储相关的idtemp = employees[:] # 深度复制# 第一遍遍历加入所有的idwhile temp != []:temp_employee = temp.pop(0) # 排在第一的不一定是最高领导,乱序[cur_id, weight, sub_lst] = temp_employee.id,temp_employee.importance, temp_employee.subordinates if cur_id in ids:ids += sub_lst# 第二遍遍历计算weight_sumwhile employees != []:employee = employees.pop(0)[cur_id, weight, sub_lst] = employee.id, employee.importance, employee.subordinates if cur_id in ids:weight_sum += weightreturn weight_sum

这种都是思想含有漏洞的解法,正确、全面的思路应当是结合map数据结构进行。

想到用字典/map结构来进行数据处理,问题将变得非常简单:

"""
# Employee info
class Employee:def __init__(self, id, importance, subordinates):# It's the unique id of each node.# unique id of this employeeself.id = id# the importance value of this employeeself.importance = importance# the id of direct subordinatesself.subordinates = subordinates
"""
class Solution:def getImportance(self, employees, id):""":type employees: Employee # 是个列表,列表中的数据类型是Employee:type id: int:rtype: int"""dic = {} # 用于列表变字典total = 0for e in employees:dic[e.id] = [e.importance, e.subordinates]ids = [id] # 用于模拟queuewhile ids != [] :cur_id = ids.pop(0)total += dic[cur_id][0]ids += dic[cur_id][1]return total

一旦列表编程以id作为键,重要性和下属作为值以后,就可以顺藤摸瓜,再用队列来走一遍,此时字典已经就位,就可以按照直接下属关系来游走了。

开始想建立一棵树,但是很麻烦,也没有想清楚细节,结合字典来处理,建树也变得简单起来。

END.

Leetcode 690相关推荐

  1. LeetCode 690. 员工的重要性(图的DFSBFS)

    文章目录 1. 题目 2. 解题 2.1 DFS 2.2 BFS 1. 题目 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是 ...

  2. leetcode 690. 员工的重要性(dfs)

    给定一个保存员工信息的数据结构,它包含了员工 唯一的 id ,重要度 和 直系下属的 id . 比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导.他们相应的重要度为 15 , 10 , ...

  3. LeetCode题解目录

    最新更新于2020.11.27 前往LeetCode主页. 前往GitHub源码.(服务器原因,暂停同步.) 前往码云主页. 已解决 456/1878 - 简单353 中等 90 困难 13 2020 ...

  4. Leetcode每日一题:690.employee-importance(员工的重要性)

    思路:找下属,求重要性和直接用BFS即可,关键是这里的数据结构不是链表,如何最快速度找到下属是最重要的:这里我用map将每个员工的id-下标索引存储起来,直接通过id得到其员工属性: int getI ...

  5. Leetcode每日必刷题库第80题,如何在不使用外部空间的情况下对有序数组去重?

    LeetCode的第80题,有序数组去重II(Remove Duplicates from Sorted Array II). 这题的官方难度是Medium,通过率是43.3%,点赞1104,反对69 ...

  6. LeetCode MySQL 1212. 查询球队积分

    文章目录 1. 题目 2. 解题 1. 题目 Table: Teams +---------------+----------+ | Column Name | Type | +----------- ...

  7. C#LeetCode刷题-哈希表

    哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串   24.2% 中等 18 四数之和   ...

  8. LeetCode github集合,附CMU大神整理笔记

    Github LeetCode集合 本人所有做过的题目都写在一个java项目中,同步到github中了,算是见证自己的进步.github目前同步的题目是2020-09-17日之后写的题.之前写过的题会 ...

  9. [LeetCode]135.Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

最新文章

  1. Tensorflow 变量的共享
  2. 汇编语言--div指令
  3. MySQL explain
  4. VTK:PolyData之IsoLines
  5. linux下shell脚本论文,Linux下Shell脚本编程
  6. 巴西政府考虑用微软产品替换开源软件
  7. 控制上网!!!(版本之1.0)
  8. kafka实战教程(python操作kafka),kafka配置文件详解
  9. 转帖 美国 工程索引 收录中国科技论文的最新规定
  10. 一级建造师-通信-五种施工顺序-口诀
  11. java编程题身高排队,试题 算法训练 预测身高
  12. 论文写不下去时怎么办?
  13. 2020年11月4日
  14. 拓嘉辰丰:拼多多差异化运营,做特色店铺
  15. c语言中百分号后面跟的数字_C语言中的各种百分号都代表什么意思
  16. htmla标签下划线去除_html超链接去掉下划线 html去除取消超链接下划线
  17. C语言练习系统 打印三角形
  18. IBMV7000存储电源模块PSU报错“Power Supply Fault type 2“
  19. 国科大学习资料--最优化计算方法(王晓)--第一次作业答案
  20. 项目记录-”海迪康”ipcamera客户端开发纪实

热门文章

  1. 数字化方法基础(一)_基础操作与生成四面体
  2. stl之vector的应用
  3. java 面试题 生产者 消费者_面试大厂必看!就凭借这份Java多线程和并发面试题,我拿到了字节和美团的offer!...
  4. java获取达梦数据库_记一次对达梦数据库的优化过程
  5. 考研c语言复试常问问题,2018考研复试常问的十个问题及回答指导
  6. python心跳包原理_Python 用心跳(UDP包)探测不活动主机
  7. 超级计算机 500,191台超算500强排名分布区间:前百强4台,前两百强31台
  8. oracle crm客户关系管理资料下载_悟空CRM:使用CRM系统进行客户关系管理的要点...
  9. MySQL中锁的必要性_MySQL中的锁之一:锁的必要性及分类
  10. ccf 路径解析 java_CCF 201604-3 路径解析