class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int importance = ;
map<int, Employee*> MAP;
for (auto em : employees)
{
MAP.insert(make_pair(em->id, em));
}
map<int, Employee*>::iterator iter;
iter = MAP.find(id);
queue<Employee*> Q;
if (iter != MAP.end())
{
int id = iter->first;
Employee* em = iter->second;
Q.push(em); while (!Q.empty())
{
Employee* emp = Q.front();
Q.pop();
importance += emp->importance;
for (auto subid : emp->subordinates)
{
map<int, Employee*>::iterator subiter;
subiter = MAP.find(subid);
if (subiter != MAP.end())
{
Q.push(subiter->second);
}
}
}
} return importance;
}
};

本题是分支限界法,广度优先搜索,使用map加速查询,防止超时。

leetcode690的更多相关文章

  1. [Java]LeetCode690. 员工的重要性 | Employee Importance

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

  2. Leetcode690.Employee Importance员工的重要性

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

  3. leetcode 78,236,300

    ---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...

随机推荐

  1. 003——VUE操作元素属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. SurfaceView基本使用--动态画正弦函数

    package com.zzw.TestSurfaceView; import android.content.Context; import android.graphics.Canvas; imp ...

  3. Struts04---命名空间的查询顺序以及默认执行的Action

    01.创建login.jsp <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  4. LeetCode OJ:Search Insert Position(查找插入位置)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. Java基础学习-抽象类

    package abstractclass; /* * 抽象类的概述: * 1.抽象类用abstract来修饰方法或者类 * 2.没有具体的方法体的方法便是抽象方法. */ class CF{ pub ...

  6. Http权威指南(概述篇总结)

    之前的<锋利的jQuery>后面陆续翻完了,实在觉得没什么值得记录的,也就没继续写了,然后看见书架上有 本去年买的<Http权威指南>,其实做web编程的,对于Http协议还是 ...

  7. Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件

    古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...

  8. ng 监听数据的变化

    $scope.$watch('监听的变量的名称',func) 在angularJs之所以能够实现绑定,是因为angularJS框架在背后为每一个模型数据添加了一个监听,与$watch其实是一个道理. ...

  9. Photon Cloud Networking: OnPhotonSerializeView Not Firing

    Photon Cloud Networking: OnPhotonSerializeView Not Firing http://answers.unity3d.com/questions/31305 ...

  10. 数据展示Matplotlib

    主要内容是Matplotlib库的基本使用和方法 1 Matplotlib库 1.1 Matplotlib的介绍 Python优秀的数据可视化第三方库 数据可视化就是将数据以特定的图形图像的方式展示出 ...