美文网首页
690. Employee Importance

690. Employee Importance

作者: 安东可 | 来源:发表于2018-03-26 22:20 被阅读3次

690. Employee Importance
【思路】:

  1. 遍历
    BFS:
        unordered_map<int, Employee*>m;
        for(auto x: employees) m[x->id] = x;
        int sum = 0;
        deque<Employee*>q;
        q.push_back(m[id]);
        while(!q.empty()){
            auto p = q.front();
            q.pop_front();
            for(auto x: p->subordinates) q.push_back(m[x]);
            sum += p->importance;
        }
        return sum;

DFS:

    int getImportance(vector<Employee*> employees, int id) {
        unordered_map<int, Employee*>m;
        for(auto x: employees) m[x->id] = x;
        int sum = 0;
        DFS(m, id, sum);
        return sum;
    }
    
    void DFS(unordered_map<int, Employee*>& m, int id, int& sum){
        sum += m[id]->importance;
        for(auto x: m[id]->subordinates) DFS(m, x, sum);
    }

相关文章

网友评论

      本文标题:690. Employee Importance

      本文链接:https://www.haomeiwen.com/subject/gyddcftx.html