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;
    }
};
上一篇:Armijo条件,Wolfe条件,Goldstein条件


下一篇:Event.RESIZE 的事件不触发