码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode690

时间:2018-09-30 13:02:51      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:使用   queue   class   end   tco   pre   return   solution   int   

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

标签:使用   queue   class   end   tco   pre   return   solution   int   

原文地址:https://www.cnblogs.com/asenyang/p/9728784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!