其他分享
首页 > 其他分享> > lc 690

lc 690

作者:互联网

class Solution {
public:
    int getImportance(vector<Employee*> employees, int id) {
        if (employees.size() == 0) {
            return 0;
        }
        unordered_map<int, Employee*> m;
        for (auto employee: employees) {
            m[employee->id] = employee;
        }    
        vector<int> total_subs = m[id]->subordinates;
        int result = m[id]->importance;
        for (int i = 0; i < total_subs.size(); i++) {
            Employee* tmp = m[total_subs[i]];
            // if this employee has subordinates, add into total_subs
            if (0 != tmp->subordinates.size()) {
                total_subs.insert(total_subs.end(), tmp->subordinates.begin(),
                        tmp->subordinates.end());
            }
            // add importance
            result += tmp->importance;
        }
        return result;
    }
};

标签:tmp,690,lc,subordinates,employee,total,id,subs
来源: https://blog.csdn.net/weixin_36149892/article/details/116329978