leetcode690

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode690相关的知识,希望对你有一定的参考价值。

class Solution {
public:
    int getImportance(vector<Employee*> employees, int id) {
        int importance = 0;
        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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode690

[leetcode-690-Employee Importance]

LeetCode - 690. Employee Importance

[LeetCode&Python] Problem 690. Employee Importance

LeetCode690. 员工的重要性(BFS || DFS)

JS Leetcode 690. 员工的重要性 题解分析