zoukankan      html  css  js  c++  java
  • 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加速查询,防止超时。

  • 相关阅读:
    demo
    Git
    rest-framework框架 -- 认证权限流程源码
    sublime
    restful 协议 +面试
    Django的CBV
    vue 之 vue-router
    vue 之node.js 02
    浏览器内核
    安装apache+php+mysql
  • 原文地址:https://www.cnblogs.com/asenyang/p/9728784.html
Copyright © 2011-2022 走看看