问题描述:简单型,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. 一个简单的Java web服务器实现
  2. 字符流复制Java文件
  3. oracle 11g的audit导致system表空间快速增长的问题
  4. python 顺序表
  5. 偶遇拍外景的小姐姐们
  6. rk3399_android7.1关于secureboot操作说明
  7. memcached全面剖析–memcached的删除机制和发展方向
  8. Atheros AR9285 坑爹网卡只有 54M/65M,开启 150M 速率的方法
  9. 谷歌chrome浏览器打不开网页,但是其他浏览器可以打开怎么办?
  10. client-error-not-possible Ubuntu连接局域网打印机
  11. android实现号码归属地,Android手机号码归属地的查询
  12. pip安装报错:There was a problem confirming the ssl certificate
  13. 华为RH2288H V3服务器更换内存条
  14. mysql 异常码1903_Mysql 异常。 寻求帮助
  15. html5 css3 图片画廊,js和CSS3 3D立方体图片画廊特效
  16. [2011JMAA]Remarks on the regularity criteria for generalized MHD equations
  17. css中脱离标准流的三种方式,CSS——脱离标准流方法一:浮动
  18. 神经网络为什么需要加偏置项(bias)?
  19. SpringBoot 如何进行限流?老鸟们都这么玩的!
  20. html伪类鼠标悬停,实现鼠标悬停Tooltip效果的CSS3代码

热门文章

  1. centos7默认字体_如何更换CentOS(Linux)系统默认字体?
  2. 北大计算机最好的班叫什么,中国大学计算机最好的班,再次迎来“图灵奖”导师,赶超“姚班”...
  3. uuid会重复吗_UUID的版本你知道吗
  4. python有什么证可以考1002python有什么证可以考_离python二级考还有十几天,吓的我赶紧买了本python教程...
  5. linux 内核参数优化 mysql_Linux记录-mysql参数优化
  6. mysql 模糊查询 s_MySql反向模糊查询
  7. TensorFlow2实现空间自适应归一化(Spatial Adaptive Normalization, SPADE)
  8. java se面试题_Java SE 8面试问答(第1部分)
  9. Java SE 9:Stream API的改进
  10. apple_Apple WWDC 2018主题总结